Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Just another Perl shrine
 
PerlMonks  

Re^2: Strange regex to test for newlines: /.*\z/

by Ojosh!ro (Beadle)
on May 21, 2007 at 13:47 UTC ( #616560=note: print w/ replies, xml ) Need Help??


in reply to Re: Strange regex to test for newlines: /.*\z/
in thread Strange regex to test for newlines: /.*\z/

I don't think it's a bug.

When the match is in /m mode .* will match anything BUT a newline. ( when in /s mode .* will match anything )
I assume what it is trying to match is one line.

So basically what this test does is :
"Between all characters (on this line) that are not newlines, and the end of the string, are there any other characters?", if so, it won't match. If it doesn't match, the only character that can cause it is a newline.
It does sound a bit like a roundabout way to get what you want though.
How about if ( $foo !~ /\n\z/ )

BTW. setting $/ has no influence on /m or /s whatsoever?
Not that I could find with experimentation.

if( exists $aeons{strange} ){ die $death unless ( $death%2 ) }


Comment on Re^2: Strange regex to test for newlines: /.*\z/
Download Code
Re^3: Strange regex to test for newlines: /.*\z/
by moritz (Cardinal) on May 21, 2007 at 14:02 UTC
    .* will match anything but a newline, or the empty string.

    So I'd expect "foo\n" =~ /.*\z/; to match, but capture the empty string in $&, not "foo\n".

    Of course there are more elaborate ways to match for a newline character ;-)

      .* will match anything but a newline, or the empty string.

      It will match both of those, actually, as it should.

      506 $ perl -we'print "yes" if "" =~ /.*/' yes 507 $ perl -we'print "yes" if "\n" =~ /.*/' yes
Re^3: Strange regex to test for newlines: /.*\z/
by Mutant (Priest) on May 21, 2007 at 14:02 UTC
    One problem tho, the following all match the string "\n":
    /.*/ /\z/ /.{0}\z/
    It's possible that \z is meant to introduce some specialness when combined with .* (or possibly some other quantifiers), but I haven't seen it mentioned in any docs. This is either a bug, or a very poorly documented feature.
Re^3: Strange regex to test for newlines: /.*\z/
by betterworld (Deacon) on May 21, 2007 at 14:32 UTC
    According to your reasoning, the first of the following one-liners shouldn't print anything either:
    $ perl -lwe 'print "match" if "foo\n" =~ /[^\n]*\z/' match $ perl -lwe 'print "match" if "foo\n" =~ /.*\z/'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-04-18 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls