Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

by Mutant (Priest)
on May 21, 2007 at 15:55 UTC ( #616597=note: print w/replies, xml ) Need Help??

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

Fair enough, but try:
perl -e 'print "match\n" if "foo\n" =~ /.{0,}\z/'
AFAIK, .* and .{0,} should be exactly equivilent, but when combined with /z they are not, if the string ends in a newline.

There definitely appears to be a bug here, but it may be that the above snippet should not match, rather than the version with .* matching.

Replies are listed 'Best First'.
Re^4: Strange regex to test for newlines: /.*\z/
by xicheng (Sexton) on May 21, 2007 at 16:57 UTC
    hmm, Just notice that, thanks..

    I think, .* and .{0,} at the beginning of a regex pattern shold have been treated as optional, so that /.*A/ and /.{0,}A/ should be the same as /A/ which means .* and .{0,} are completely unnecessary in the above patterns..

    But \z looks behave very differently to .* and .{0,} as you mentioned.

    This looks like a Perl-related problem, PHP(use a similar regex engine) does it pretty well:
    php -r ' $str = "foo\n"; if (preg_match("/.*\z/", $str)) { print "match\n"; } ' match
    Probably it's a bug, and I am waiting for someone to make it clear. :-)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://616597]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-06-22 09:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (123 votes). Check out past polls.