in reply to Help with Regular Expression matching

So you want to match some text which might be surrounded by brackets?

Just match against "<?" and ">?" for the bracket zero or more times.

($match) = $string =~ /<?(ab)>?/;
Steve
---
steve.org.uk

Replies are listed 'Best First'.
Re: Re: Help with Regular Expression matching
by Anonymous Monk on Feb 18, 2004 at 10:20 UTC
    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).
        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.