Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: This regexp made simpler

by BrowserUk (Patriarch)
on Apr 25, 2010 at 11:08 UTC ( [id://836765]=note: print w/replies, xml ) Need Help??


in reply to This regexp made simpler

printf( "\n$_: " ), m[A( [^Z]+)Z] and print "'$1'" for 'AZ', 'A SOMETHING Z', 'ASOMETHINGZ', 'A Z';; AZ: A SOMETHING Z: ' SOMETHING ' ASOMETHINGZ: A Z: ' '

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.

Replies are listed 'Best First'.
Re^2: This regexp made simpler
by AnomalousMonk (Archbishop) on Apr 25, 2010 at 11:23 UTC

    But that doesn't match 'AZ', which the OP seems to require, and also doesn't match 'A Z' (single space between first and final characters), which also seems to be required.

      A simple variation fixes that:
      /A( [^Z]*)?Z/

      It surprises me how many monks in this thread seem to think that expressing the "no Z between ..." condition with .*? is a good idea.

        Indeed. That was the main point I was going to contribute, but you've beat me to it.

        The OP even specifically says he wants to match "any number of characters not containing Z", which is properly written [^Z]*. .*?Z means "the shortest available sequence of any character at all (except, usually, newlines), followed by a Z", which is not quite the same thing.

        But doesn't that still give you an undefined  $1 that needs to be 'fixed' sooner or later?

        >perl -wMstrict -le "'AZ' =~ /A( [^Z]*)?Z/; print qq{'$1'}; " Use of uninitialized value $1 in concatenation (.) or string at ... ''

        'Elegance', admittedly protean, would yet seem to require avoiding this extra step.

        It surprises me how many monks in this thread seem to think that expressing the "no Z between ..." condition with .*? is a good idea.
        Maybe because it saves you from writing Z twice, which can be a maintenance issue? This is true especially if Z happens not to be just a latter, but a more complicated pattern.

        -- 
        Ronald Fischer <ynnor@mm.st>

      Ah yes, missed that. Maybe this improved version.

      printf( "\n$_: " ), m[A( [^Z]*|)Z] and print "'$1'" for 'AZ', 'A SOMETHING Z', 'ASOMETHINGZ', 'A Z', 'A Z';; AZ: '' A SOMETHING Z: ' SOMETHING ' ASOMETHINGZ: A Z: ' ' A Z: ' '

      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: note [id://836765]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-23 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found