Re: PERL regex modifiers for m//
by moritz (Cardinal) on Nov 28, 2011 at 11:27 UTC
|
I'm pretty sure that "Mastering Regular Expressions" by Jeffrey Friedl has extensive examples, and perlretut also has some; just search for "modifier" in the documnent.
| [reply] |
Re: example of 'm / / m' related example and compare to 'm / / s'
by Stamm (Sexton) on Nov 28, 2011 at 13:37 UTC
|
$s = "foo\nfoot\nroot";
$s =~ /^foo/g; # matches only the first foo
$s =~ /^foo/gm; # matches both foo
$s =~ /f.*t/g; # matches only foot
$s =~ /f.*t/gs; # matches foo\nfoot\nroot
$s =~ /f.*?t/gs; # matches foo\nfoot
$s =~ /^foot.*root$/g; # doesn't match
$s =~ /^foot.*root$/gm; # doesn't match
$s =~ /^foot.*root$/gs; # doesn't match
$s =~ /^foot.*root$/gms; # matches foot\nroot
| [reply] [d/l] |
|
Line 3: " # matches both foo": are you sure?
C:>perl -e "$s = \"foo\nfoot\nroot\";($y) = $s =~ /^foo/gm;print $y;"
foo
| [reply] [d/l] |
|
Yes, but not in the same pass.
>perl -E"say join ',', qq{foo\nfoot\nroot} =~ /^foo/gm;"
foo,foo
| [reply] [d/l] |
Re: example of 'm / / m' related example and compare to 'm / / s'
by Corion (Patriarch) on Nov 28, 2011 at 12:27 UTC
|
What part of the first section of perlre, ("Modifiers") , do you have problems with?
| [reply] |
Re: PERL regex modifiers for m//
by Khen1950fx (Canon) on Nov 28, 2011 at 11:28 UTC
|
| [reply] |
Re: PERL regex modifiers for m//
by Anonymous Monk on Nov 28, 2011 at 11:27 UTC
|
http://perldoc.perl.org/perlre.html#Modifiers
I'm sorry that I cannot make this a hyper-link, but the site admins recently prohibited posting external links as Anonymonk. | [reply] |
|
| [reply] |
Re: PERL regex modifiers for m//
by remiah (Hermit) on Dec 05, 2011 at 12:55 UTC
|
As for s and m switch, example of "little princess" in this node was very nice tutorial for me. | [reply] |
Re: example of 'm / / m' related example and compare to 'm / / s'
by ikegami (Patriarch) on Nov 28, 2011 at 18:26 UTC
|
| [reply] |
|
How can one compare two independent things?
It's not so hard:
- //m causes ^ and $ to match
at newlines in the middle of your string, while //s
causes . to match newlines.
- The Nile is a river in Africa, while the Mississippi is a river in
North America.
- The -g flag turns on debugging information, while
the -O flag turns on optimization.
| [reply] [d/l] [select] |
|
//m causes ^ and $ to match at newlines in the middle of your string, while //s causes . to match newlines.
You're comparing using /m to not using /m, and using /s to not using /s. You're not comparing /m to /s.
Your river example compares the location of the rivers, but there's no common aspect of /m and /s to compare.
| [reply] |
|
//m causes ^ and $ to match at newlines in the middle of your string, while //s causes . to match newlines.
You're comparing using /m to not using /m, and using /s to not using /s. You're not comparing /m to /s.
Your river example compares the location of the rivers, but there's no common aspect of /m and /s to compare.
| [reply] |
Re: example of 'm / / m' related example and compare to 'm / / s'
by TJPride (Pilgrim) on Nov 28, 2011 at 18:55 UTC
|
I often get confused as to which is which myself. I generally try both and then pick the one that works the way I want it to. | [reply] |
|
| [reply] [d/l] |
|
Hm. That's like advocating always taking a swimsuit & sunblock and a raincoat & umbrella cos it saves listening to the weather forecast. More than slightly ridiculous.
The very reason it is hard, even for long-time Perlers with scads of frequent regex user miles, to remember which (/s /m) does what, is because they are so rarely required.
So what could possibly be wrong with advocating their use at all times?
For a start, you're crying wolf. By using them everywhere they become the norm, and after a while people stop asking themselves why is he using that here. And that is bad.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
|
|
|