Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Regular Expressions

by chas (Priest)
on May 17, 2005 at 21:21 UTC ( [id://458004]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Regular Expressions
in thread Regular Expressions

In the first case, there is nothing stored in \1 so there shouldn't be any match (and it is an error.) In the latter case, \1 probably refers to the outer parentheses, but that hasn't taken effect yet, so there is no match and no error (perhaps it really doesn't make sense but perl tries to interpret it that way...)
If you instead do
print "Match: $1\n" if $test_string =~ /((?:f))\1/;
the result is: Match: f
(Doing that doesn't seem especially useful, though...)
chas

Replies are listed 'Best First'.
Re^4: Regular Expressions
by Nkuvu (Priest) on May 17, 2005 at 21:37 UTC
    Allow me to clarify a bit.

    What I find interesting is that for the first case perl doesn't warn about it at all*. If I have a regex that uses backreferences but has no capturing parens, I'd expect that perl (with use warnings enabled) would mention something about it.

    I'm not saying it's wrong behavior, and I understand why it's not matching. It just didn't provide the warning I'd expect.

    *update: Or put it another way. I mean that the error message "Reference to nonexistent group in regex;" goes away if the regex is changed slightly. Consider:
    print "matches\n" if $test_string =~ /(?:f)\1(.)/;
    There is a valid set of capturing parens there, but after the \1 backreference is used. So this won't match -- and Perl doesn't warn about it. Surprised me a bit.

      Yes, it is a bit surprising/confusing that there isn't a warning. I was just trying to guess why there isn't an error message ("nonexistent group") as in the previous case, and maybe it is for the reason I stated (i.e. \1 refers to the outer parens);I'm really not convinced, though.
      chas

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-16 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found