Searching for the first komma in a sentence

F

Francois Massion

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

Replacement expression:

$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?
 
N

Nathan Keel

Francois said:
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

Replacement expression:

$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?

See ^, [^,] and *.? and the options you have for using them. (^ start of
string. [^,] negate (anything but), and *.? non greedy.
 
J

Jürgen Exner

[Searching for the first komma in a sentence]

See
perldoc -f index
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.

my $i = index $old, ',';
my $new = substr ($old, $index) . substr($old, 0, $index);
#Note: untested, probably off by 1 for the first or the second slice

jue
 
S

sln

[Searching for the first komma in a sentence]

See
perldoc -f index
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.

my $i = index $old, ',';
my $new = substr ($old, $index) . substr($old, 0, $index);
#Note: untested, probably off by 1 for the first or the second slice

jue

Yeah, but can you do it without moving the ',' with the half?
You can, but your doing a lot of math. When you try to do 2 ','
the arithametic gets too nasty to think about.

-sln
 
F

Francois Massion

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?

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

It was as simple as that! Thanks for the help. It works perfectly.
$sentence =~ s/(.*?)(,)(.*)/$3$2$1/;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top