Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Regex - substitute

by Anonymous Monk
on Feb 17, 2003 at 03:23 UTC ( #235869=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks, I'm here to seek your wisdom. Try to learn the Perl Regex feature, I tried a few examples, and came across this weird thing(?), and cannot find any good answer: The example is like this:

 $s = "goodwood";

 $s =~ s/o*/e/;  

 print "$s \n";  

# got - 'egoodwood'

It really stuck me - since there is no match, why the 'e' got prefixed into the string? I understand that o* - can match nothing or more, but should the * quantifier be greedy and match the 'oo' in the good? Thanks.

Comment on Regex - substitute
Select or Download Code
Re: Regex - substitute
by Paladin (Priest) on Feb 17, 2003 at 03:30 UTC
    Perl RE's match "leftmost, longest". "Leftmost" takes precedence over "longest". /o*/ can match zero characters, as you said, and the leftmost place it finds 0 characters is right at the start of the string.
Re: Regex - substitute
by Enlil (Parson) on Feb 17, 2003 at 03:33 UTC
    What happens is that the regular expression will start searching the first position of the string and attempt to match o and not finding an o it will try the other alternative at this position. The * after the o gives it the option of matching nothing. (which it does). And, since it has found a match it replaces it with an e. Regular expressions try all the possible alternatives at a position in a string before moving on to the next position (left most first). so it never reaches the oo part as it can match nothing much earlier.

    -enlil

Re: Regex - substitute
by graff (Chancellor) on Feb 17, 2003 at 03:38 UTC
    To get the match that you're looking for, use the "+" instead of the "*":
    s/o+/e/;
    will turn "goodwood" into "gedwood" (only the first occurrence is replaced); "+" refers to "one or more". To replace all occurrences of "o+" in the string, use the "g" modifier as well:
    s/o+/e/g;
    will turn "goodwood" into "gedwed".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2015-07-03 08:18 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 (49 votes), past polls