in reply to conditional match in regex
Well, here's how I would do it; this regex will populate $1 with the sigil, and $2 with the name if it is a variable like "$foo"; in the "<foo>" case, $1 will be undef and $2 will contain the name.
/ ^ # start (?:([\$\@\*\%])|<) # leading sigil or < (\w+) # name (?(?{$1}) |> ) # if there was a leading sigil, match nothing; # otherwise, match > $ # end /x;
Another way might be to use this one:
/ ^ # start ([\$\@\*\%<]) # sigil (\w+) # name (?(?{$1 eq '<'}) >| ) # if the sigil is a '<', match the end; # otherwise match nothing $ # end /x;
This is different in that in the "<foo>" case, $1 will be '<'.
In either case, theres no real reason to capture $3, since there is only one possibility that it could be(>), and you will know if the possibility is true or not depending on $1.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: conditional match in regex
by petral (Curate) on Nov 05, 2002 at 01:23 UTC | |
by jryan (Vicar) on Nov 05, 2002 at 21:47 UTC | |
by petral (Curate) on Nov 05, 2002 at 23:08 UTC | |
by jryan (Vicar) on Nov 06, 2002 at 08:09 UTC | |
by petral (Curate) on Nov 06, 2002 at 14:58 UTC | |
| |
Re: Re: conditional match in regex
by John M. Dlugosz (Monsignor) on Nov 05, 2002 at 16:31 UTC | |
by jryan (Vicar) on Nov 05, 2002 at 19:56 UTC |