Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to ask another regex question. Consider this string:
$x = 'ab cd cd EF ghi jkl'; $x =~ /cd (.*) ghi/;$1 evaluates to "cd EF". This looks correct.
But:
$x =~ /cd (.*?) ghi/;$1 evaluates to "cd EF", same as above. A strange result, as I expect it to be just "EF".
Why do matches aginst (.*) and (.*?) result in exactly the same result? What needs to be done to get only the "EF" as a result?
In real life the EF is a random text that I want to extract.
Thank you.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex problem - (non)greedy?
by ikegami (Patriarch) on Nov 14, 2013 at 14:18 UTC | |
|
Re: Regex problem - (non)greedy?
by hdb (Monsignor) on Nov 14, 2013 at 14:29 UTC | |
by ikegami (Patriarch) on Nov 14, 2013 at 14:31 UTC | |
by hdb (Monsignor) on Nov 14, 2013 at 14:34 UTC | |
by ikegami (Patriarch) on Nov 14, 2013 at 14:38 UTC | |
by hdb (Monsignor) on Nov 14, 2013 at 14:40 UTC | |
| |
by LanX (Saint) on Nov 14, 2013 at 14:37 UTC | |
|
Re: Regex problem - (non)greedy?
by vpbamberg (Initiate) on Nov 14, 2013 at 15:13 UTC | |
|
Re: Regex problem - (non)greedy?
by Laurent_R (Canon) on Nov 14, 2013 at 18:22 UTC |