Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^5: /g option not making s// find all matches (updated)

by AnomalousMonk (Archbishop)
on May 31, 2018 at 14:10 UTC ( [id://1215543]=note: print w/replies, xml ) Need Help??


in reply to Re^4: /g option not making s// find all matches (updated)
in thread /g option not making s// find all matches

... if you define your default to be /xms, in code with nine regexes ending in /xms and a tenth ending in /xs ...

But that's the point: you never use anything other than an /xms tail in your own code. If you're dealing with someone else's code, you're on your own, and you may have much bigger problems than just regexes to contend with; that's the way of the world.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^6: /g option not making s// find all matches (updated)
by raygun (Scribe) on May 31, 2018 at 20:09 UTC
    But that's the point: you never use anything other than an /xms tail in your own code.
    I dunno, that seems unnecessarily rigid. A "best practice" should mean "do this unless there's a good reason to do otherwise," not "always blindly do this no matter what." All three of those options modify the regex behavior. What if you need the unmodified behavior?

    I realize "need" might be too strong a word; with /x, for instance, you can always just escape any literal space characters your regex needs. But if there are several of them, and your regex is otherwise simple, all those escapes clutter the code more than just omitting the /x. And someone else encountering m/a\ b\ c/xms in your code will wonder what tricky thing you're trying to do by telling the regex engine to ignore whitespace and then escaping all your whitespace.

    I'm not saying your system is wrong — I'm certain it serves you well — but I don't think I'm sold on it yet.
      What if you need the unmodified behavior?

      But what unmodified behavior would you need that you would not be able to access in a fairly clear and simple manner? E.g., if you're using /s and you want the "match anything except a newline" behavior, does not  [^\n] best express this? (One of the driving motivations of TheDamian's regex PBPs is clarity of expression.)

      Conversely, in the absence of /s, how would you best express "match anything at all, including a newline" (the most common use-case, IMO)? Something like  [\s\S] would do the trick, but is that anyone's idea of clarity? (And don't get me started on (?s:.) ...)

      ... with /x ... you can always just escape any literal space characters your regex needs. But ... all those escapes clutter the code ...

      I must admit that the need for special handling of spaces can be annoying. But even this can advance clarity: [ ] (I would not use \) matches a blank space exactly; [ \t] matches a blank space or a tab exactly; \s matches any whitespace exactly; etc... (You may object that \s is too general, but there've been several occasions on which I've been saved from a 3 AM phone call by having used \s rather than a more specific [ ] — or even \) And \Q...\E can help out, too.

      Anyway, TheDamian explains it all more completely and clearly than I can, so maybe pursue that avenue.


      Give a man a fish:  <%-{-{-{-<

        if you're using /s and you want the "match anything except a newline" behavior, does not [^\n] best express this?
        In the context of /s, sure. But again, there is a larger context: someone else reading the regex will wonder at the incongruity of using /s at all in conjunction with a bunch of [^\n]s, when omitting the /s would simplify the whole thing (and also make the regex syntax match that used by most other regex engines, a consideration — albeit minor — when aiming for clarity).
        One of the driving motivations of TheDamian's regex PBPs is clarity of expression.
        A goal we can all agree with! Now, enter devils and details.
        Anyway, TheDamian explains it all more completely and clearly than I can, so maybe pursue that avenue.
        Point taken. I'm sure my perl experience is a tiny fraction of either yours or his.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found