in reply to Re: Help with Regular Expression matching
in thread Help with Regular Expression matching

This is a good suggestion, but I need to match /ab/ only when it surrounded or not surrounded by brackets, not where there is a bracket on only one side (as the <? and >? approach would allow).

Replies are listed 'Best First'.
Re: Help with Regular Expression matching
by skx (Parson) on Feb 18, 2004 at 10:27 UTC
      This will capture <ab> into $1 and ab into $2 - This means that $match will not capture ab.

      As our friend notes, you mean:

      my ($match) = $string =~ /(<ab>|ab)/;
      Update: Ok, I didn't read the spec carefully enough, I think this will do it:
      my ($match) = $string =~ /(?!<ab[^>]|[^<]ab>)<?(ab)>?/
      But I'd probably split it into two regexes.

      I'm talking twaddle, anyone else want a go?


      davis
      It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
        This is close, but I don't want to capture the surrounding brackets.