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

Re: Regex Bug?

by graff (Chancellor)
on May 26, 2002 at 05:58 UTC ( [id://169338]=note: print w/replies, xml ) Need Help??


in reply to Regex Bug?

Well, this does seem curious:
$ perl -v This is perl, v5.6.1 built for i586-linux ... $ perl -e '$u="http://www.domain.com/hi.html?a=b&c=d"; print $u,$/; $u=~s/(.*?)\?(.*?)/$1/gs; print $u,$/;'
produces:
http://www.domain.com/hi.html?a=b&c=d http://www.domain.com/hi.htmla=b&c=d
But you said that you only wanted the first part (before the "?"), so why did you put parens around the second part? It does work as desired this way:
$ perl -e '$u="http://www.domain.com/hi.html?a=b&c=d"; print $u,$/; $u=~s/(.*?)\?.*/$1/; print $u,$/;' http://www.domain.com/hi.html?a=b&c=d http://www.domain.com/hi.html
For that matter, whether or not you use the following part as well, why not split:
$ perl -e '$u="http://www.domain.com/hi.html?a=b&c=d"; print $u,$/; ($ub,$ue)=split(/\?/,$u,2); print "$ub :: $ue",$/;' http://www.domain.com/hi.html?a=b&c=d http://www.domain.com/hi.html :: a=b&c=d
Still, the initial example is baffling, and I hope someone can explain why it should behave the way it did, just for our communal peace of mind. The final "gs" is of course superfluous for this example -- the behavior is the same with or without those qualifiers on the regex. (And "extra strength" is not really an appropriate reason for using them, anyway; check their descriptions in perlre to see what their proper, intended functions are.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found