Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
Replies are listed 'Best First'.
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 "*":
    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:
    will turn "goodwood" into "gedwed".
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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2015-11-30 07:23 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (764 votes), past polls