Searching for the first komma in a sentence

Discussion in 'Perl Misc' started by Francois Massion, Aug 15, 2009.

  1. 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?
    Francois Massion, Aug 15, 2009
    #1
    1. Advertising

  2. Francois Massion

    Nathan Keel Guest

    Francois Massion wrote:

    >
    > 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.
    Nathan Keel, Aug 15, 2009
    #2
    1. Advertising

  3. Francois Massion <> wrote:

    [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
    Jürgen Exner, Aug 16, 2009
    #3
  4. Francois Massion

    Guest

    On Sat, 15 Aug 2009 19:53:10 -0700, Jürgen Exner <> wrote:

    >Francois Massion <> wrote:
    >
    >[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
    , Aug 16, 2009
    #4
  5. On 15 Aug., 22:09, Tad J McClellan <> wrote:
    > Francois Massion <> wrote:
    >
    > > 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.

    >
    > 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).
    >
    > > This would be the reason for this result.

    >
    > Greed would be the reason for result you are getting.
    >
    > > tell Perl to start searching for the first komma in the sentence. How
    > > can I do it?

    >
    >     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/;
    Francois Massion, Aug 16, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jluis
    Replies:
    3
    Views:
    690
    Riyaz
    May 5, 2004
  2. Geo
    Replies:
    0
    Views:
    466
  3. Ian Pilcher
    Replies:
    0
    Views:
    3,763
    Ian Pilcher
    Dec 12, 2003
  4. Replies:
    2
    Views:
    1,415
    Juan T. Llibre
    Nov 12, 2008
  5. stumblng.tumblr
    Replies:
    1
    Views:
    192
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page