in reply to A complex recursive regex,help

You don't need recursion. Just use the positive look back and look forward assertions which don't eat string. See perlman:perlre

my $a = "x|xx|xx|xxxx|xx|xxx|xx|xx|x"; $a =~ s/(?<=\|)xx(?=\|)/x x/g; print "[$a]\n"; __DATA__ [x|x x|x x|xxxx|x x|xxx|x x|x x|x]

You don't say what you want to do with the edge cases:

$a = 'xx|xx|xx'; # should this be (use example above): [xx|x x|xx] # or should it be [x x|x x|x x] # in which case you will need to add an extra regex because you can't +have # variable width lookbacks. this regex just processes those edge cases $a =~ s/^xx(?=\|)|(?<=\|)xx$/x x/g; # combined with the first part gives you some real perl line noise # if you want it all in a single regex $a =~ s/^xx(?=\|)|(?<=\|)xx(?=\|)|(?<=\|)xx$/x x/g;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: A complex recursive regex,help
by OM_Zen (Scribe) on Feb 06, 2003 at 18:53 UTC
    Hi Tachyon ,

    my $a = "x|xx|xx|xxxx|xx|xxx|xx|xx|x"; $a =~ s/(?<=\|)xx(?=(\||$))/x x/g; # this is actually doing the regex edge cases too Tachyon


      Well *actually* it does not. Your test string does not *actually* contain the edge cases ( xx at the begining and end of the string):o) If your test string had contained the edge cases you would no doubt have noted the following failure case.....

      my $a = "xx|xx|xx|xxxx|xx|xxx|xx|xx|xx"; $a =~ s/(?<=\|)xx(?=(\||$))/x x/g; print $a; __DATA__ xx|x x|x x|xxxx|x x|xxx|x x|x x|x x

      Here is one way to fix your regex:

      $a =~ s/(?<=\|)|^xx(?=(\||$))/x x/g; # and here is a way using the zero width boundary assertion \b # that will match | ^ or $ but also matches any non aphlanumeric # so would potentially fail on '|xx,xx|' type strings # if they exist in practice as your real data is no doubt # not really xx..... $a =~ s/\bxx\b/x x/g;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: A complex recursive regex,help
by OM_Zen (Scribe) on Feb 06, 2003 at 01:00 UTC
    Hi Tachyon ,

    my $a = "x|xx|xx|xxxx|xx|xxx|xx|xx|x"; $a =~ s/(?<=\|)xx(?=\|)/x x/g; print "[$a]\n";


    This is the backward and forward assertions that I needed ,man , I have to learn more on this ,man THANKS A BUNCH Tachyonfor the help

      Glad to help. The only bummer is that you cna't have variable width lookbacks (positive or negative) like (?<=x*) or (?<!x*) You can have variable width look forward assertions though (?=x*) or (?!x*) ar OK.....

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Hi Tachyon ,

        The variable width lookup stuff , I realise that after your code and the limitations too, THANKS A BUNCH man