Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Regex Pop Quiz with .*, /g, and /s

by Thelonius (Priest)
on Oct 02, 2007 at 18:13 UTC ( #642195=note: print w/ replies, xml ) Need Help??


in reply to Regex Pop Quiz with .*, /g, and /s

I'd have to say that it was not what I expected. I'm also surprised that this hasn't bitten me before. While I know theoretically that * can match empty strings, perl has always seemed to me to do the intuitive thing.

I guess I generally don't replace nothing with something. Usually when I use * I'm either just skipping over white space (or the moral equivalent), or I replace any pattern than has * in it with itself. That is, something like  s/A(.*)B/C${1}D/g;

Generally, I also try to constrain my patterns more, so I usually avoid constructs like ".*" or "*?", and would write something like s/A([^B]*)B/C${1}D/g;

The /s doesn't seem to have anything to do with it, except that, of course, you included a \n in your string. For example,

my $string = "aaab"; $string =~ s/a*/go/g;
Now string is "gogobgo".


Comment on Re: Regex Pop Quiz with .*, /g, and /s
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-07-05 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls