CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
substitution with regex (with wildcards in the middle)

 



Roger Niesten
New User

Nov 20, 2015, 5:34 AM

Post #1 of 3 (1112 views)
substitution with regex (with wildcards in the middle) Can't Post

I'm having trouble with substitutions with regex (pretty new to this subject).

I need to substitute from a specific string until the first occurrence of another specific string. When using .* for the middle part, it substitutes from the first start string until the last end string...

Example:
my $str = 'this is foo and bar in a bar in mexico';
print "\noriginal string:\n$str\n\n";
$str =~ s/(foo .* bar )/John /i;
print "\nmodified string:\n$str\n\n";

Output:
original string:
this is foo and bar in a bar in mexico
modified string:
this is John in mexico

Expected would be:
this is John in a bar in mexico

I tried several thinks, but none had the desired output..
$str =~ s/(foo .*[^bar].* bar )/John /i;

How can I achieve the expected result?
Thanks in advance!


FishMonger
Veteran / Moderator

Nov 20, 2015, 6:12 AM

Post #2 of 3 (1110 views)
Re: [Roger Niesten] substitution with regex (with wildcards in the middle) [In reply to] Can't Post

.* is a "greedy" quantifier. Meaning that it will match everything upto the end of the string then (if needed) will backtrack one character at a time until the next part of the regex matches.

You need to change it to be non greedy by adding a ? after the *.

Code
$str =~ s/(foo .*? bar )/John /i;



(This post was edited by FishMonger on Nov 20, 2015, 6:12 AM)


BillKSmith
Veteran

Nov 20, 2015, 6:56 AM

Post #3 of 3 (1104 views)
Re: [Roger Niesten] substitution with regex (with wildcards in the middle) [In reply to] Can't Post

Note that FishMonger's solution fixes an error that he did not tell you about. The character class [^bar] only matches one character. That character can be anything but a "b", an "a", or an "r".
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives