Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Excluding groups of characters in regular expressions

by suaveant (Parson)
on Dec 21, 2007 at 14:53 UTC ( [id://658457]=note: print w/replies, xml ) Need Help??


in reply to Excluding groups of characters in regular expressions

You'd be better off treating this more as a parser problem than a regexp problem, because trying to match nested items in regexp is advanced stuff... here is some code that should get you started... not ideal but easy :)
$text = "<paragraph>some <paragraph>some data</paragraph>data</paragra +ph><paragraph>more data</paragraph><paragraph>even more data</paragra +ph>"; my $depth = 0; $text =~ s{(<(/)?paragraph>)}{check($depth)}gie; print "$text\n"; sub check { if($2) { $_[0]--; if($_[0] == 0) { return $1; } $_[0] = 0 if $_[0] < 0; } else { $_[0]++; return $1 if $_[0] == 1; } return ''; }

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re^2: Excluding groups of characters in regular expressions
by semirhage (Initiate) on Dec 21, 2007 at 16:19 UTC
    A friend of mine was able to help me... here is the answer for anybody who needs help with this sort of thing in the future.

    $_='$0<paragraph>$1<paragraph>$2</paragraph>$3<paragraph>$4<paragraph> +$5</paragraph>$6</paragraph>$7</paragraph>$8<paragraph>$9</paragraph> +$10<paragraph>$11</paragraph>$12 '; ($re=$_)=~s/((<paragraph>)|(<\/paragraph>)|[^<]+|.)/${[')','']}[!$3]\Q +$1\E${['(','']}[!$2]/gs; $re=join"|",map{quotemeta}(eval{/$re/}); s{($re)}{local $_=$1;s#</?paragraph>##g;$_}eg; print;


    Tom
      Have you ever heard anyone discussing your friend's code before... and if so, was there a lot of swearing involved?

      No offense, but the code looks like an entry to an obfuscation contest. Not so bad if its a one off script, but barely maintainable if it's going to be around for a bit.

                      - Ant
                      - Some of my best work - (1 2 3)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://658457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found