Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Searching for the first komma in a sentence
- Francois Massion
August 15, 2009, 6:07 pm
rate this thread
I am trying to modify the pattern of a sentence with a komma
separating different sections of the sentence. I would like to put at
the end of the sentence what is at the beginning of the sentence.
Current pattern: word1 word2 word3, word4, word5 word6
Expected pattern: word4, word5 word6, word1 word2 word3
$sentence =~ s/(.*)(,)(.*)/$3$2$1/;
Actual result: word5 word6,word1 word2 word3, word4
Apparently Perl doesn't start at the beginning of the sentence but
searches backwards. This would be the reason for this result. As some
sentences have only one komma, others more than 2, I would like to
tell Perl to start searching for the first komma in the sentence. How
can I do it?
Re: Searching for the first komma in a sentence
No, perl _does_ start at the beginning, but it has 2 possible
matches for the first .*
word1 word2 word3, word4, word5 word6
It must choose one. By default it chooses the longest one (it is greedy).
Greed would be the reason for result you are getting.
perldoc -q greedy
What does it mean that regexes are greedy? How can I get around it?
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"