Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
When writing a regex that has multiple patterns in it, it would be nice to know which of the patterns matched. I've looked around and can't seem to find what I'm looking for.
For example:
What I get is:
Any thoughts?
$pat1="field"; $pat2="f.i.e.l.d"; $pat3="the"; $str = "There are many soccer fields in England - f1i2e3l4d"; while($str =~ m/($pat1|$pat2|$pat3)/ig){ print "Found '$1' from pattern ??\n"; }
What I get is:
What I'd like is to find the syntax to replace ?? with the matching pattern:Found 'The' from pattern ?? Found 'field' from pattern ?? Found 'f1i2e3l4d' from pattern ??
Found 'The' from pattern 'the' Found 'field' from pattern 'field' Found 'f1i2e3l4d' from pattern 'f.i.e.l.d'
Any thoughts?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Determining which pattern matched
by rubasov (Friar) on Apr 02, 2010 at 13:40 UTC | |
by ikegami (Patriarch) on Apr 05, 2010 at 22:23 UTC | |
by rubasov (Friar) on Apr 05, 2010 at 22:42 UTC | |
by ikegami (Patriarch) on Apr 06, 2010 at 00:24 UTC | |
Re: Determining which pattern matched
by Fletch (Bishop) on Apr 02, 2010 at 13:38 UTC | |
by Anonymous Monk on Apr 02, 2010 at 23:02 UTC | |
Re: Determining which pattern matched
by FunkyMonk (Chancellor) on Apr 02, 2010 at 13:46 UTC | |
Re: Determining which pattern matched
by BrowserUk (Patriarch) on Apr 02, 2010 at 13:52 UTC | |
Re: Determining which pattern matched
by CountZero (Bishop) on Apr 02, 2010 at 15:21 UTC | |
Re: Determining which pattern matched
by Anonymous Monk on Apr 05, 2010 at 22:00 UTC |
Back to
Seekers of Perl Wisdom