Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

PERL regex modifiers for m//

by rockstar99 (Novice)
on Nov 28, 2011 at 11:15 UTC ( [id://940352]=perlquestion: print w/replies, xml ) Need Help??

rockstar99 has asked for the wisdom of the Perl Monks concerning the following question:

Could someone please give an example of 'm / / m' related example and compare to 'm / / s' example. Thanks in advance.

Replies are listed 'Best First'.
Re: PERL regex modifiers for m//
by moritz (Cardinal) on Nov 28, 2011 at 11:27 UTC
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
      Line 3: " # matches both foo": are you sure?
      C:>perl -e "$s = \"foo\nfoot\nroot\";($y) = $s =~ /^foo/gm;print $y;" foo
        Yes, but not in the same pass.
        >perl -E"say join ',', qq{foo\nfoot\nroot} =~ /^foo/gm;" foo,foo
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?

Re: PERL regex modifiers for m//
by Khen1950fx (Canon) on Nov 28, 2011 at 11:28 UTC
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.

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.
Re: example of 'm / / m' related example and compare to 'm / / s'
by ikegami (Patriarch) on Nov 28, 2011 at 18:26 UTC

    "m" affects "^" and "$".

    "s" affects "."

    How can one compare two independent things?

      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.

        //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.

        //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.

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.
      I often get confused as to which is which myself.

      This is the point of TheDamian's injunctions in Perl Best Practices (PBP) to always use the /m and /s (and /x) modifiers (BPs 148 and 151 – and 147) in every match and regex object definition:  . ^ $ always behave the same way and confusion is at least reduced if not eliminated.

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://940352]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-19 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found