Multiple Match of regular exp...

Discussion in 'Perl Misc' started by stratus, May 6, 2005.

  1. stratus

    stratus Guest

    If I want to replace "-test" with "-test OK", and if it already contains "-test OK", it do not need to add it again.

    Hence, String "-test" ========> will become ======> "-test OK"
    String "-test 1234 -test 456" =====> "-test OK1234 -test OK456"
    String "-test OK -test 45 -test OO" ====> "-test OK -testOK45 -test OKOO"


    How to do this?
     
    stratus, May 6, 2005
    #1
    1. Advertising

  2. stratus wrote:
    > If I want to replace "-test" with "-test OK", and if it already contains "-test OK", it do not need to add it again.
    >
    > Hence, String "-test" ========> will become ======> "-test OK"
    > String "-test 1234 -test 456" =====> "-test OK1234 -test OK456"
    > String "-test OK -test 45 -test OO" ====> "-test OK -testOK45 -test OKOO"
    >
    >
    > How to do this?


    $ perl -le'
    my @x = ( "-test", "-test 1234", "-test 456", "-test OK", "-test 45", "-test
    OO" );
    for ( @x ) {
    print "Before: $_";
    s/-test\s*(?! ?OK)/-test OK/;
    print " After: $_";
    }
    '
    Before: -test
    After: -test OK
    Before: -test 1234
    After: -test OK1234
    Before: -test 456
    After: -test OK456
    Before: -test OK
    After: -test OK
    Before: -test 45
    After: -test OK45
    Before: -test OO
    After: -test OKOO



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 6, 2005
    #2
    1. Advertising

  3. stratus

    stratus Guest

    stratus <> ´£¨ì:
    : If I want to replace "-test" with "-test OK", and if it already contains "-test OK", it do not need to add it again.

    : Hence, String "-test" ========> will become ======> "-test OK"
    : String "-test 1234 -test 456" =====> "-test OK1234 -test OK456"
    : String "-test OK -test 45 -test OO" ====> "-test OK -testOK45 -test OKOO"


    : How to do this?


    I mean it should in a string.like
    $string="-test -test 1234 -test 456 -test OK -test 45 -test OO";
     
    stratus, May 6, 2005
    #3
  4. stratus

    Damian James Guest

    On Fri, 06 May 2005 07:21:12 GMT, John W. Krahn said:
    > $ perl -le'
    > my @x = ( "-test", "-test 1234", "-test 456", "-test OK", "-test 45", "-test
    > OO" );
    > for ( @x ) {
    > print "Before: $_";
    > s/-test\s*(?! ?OK)/-test OK/;
    > print " After: $_";
    > }
    > '


    Dang, and here I was just about to follow up with just:

    ?!
     
    Damian James, May 6, 2005
    #4
  5. stratus

    Damian James Guest

    On Fri, 06 May 2005 07:21:12 GMT, John W. Krahn said:
    > $ perl -le'
    > my @x = ( "-test", "-test 1234", "-test 456", "-test OK", "-test 45", "-test
    > OO" );
    > for ( @x ) {
    > print "Before: $_";
    > s/-test\s*(?! ?OK)/-test OK/;
    > print " After: $_";
    > }
    > '


    Dang, and here I was about to follow up with just:

    ?!
     
    Damian James, May 6, 2005
    #5
  6. stratus wrote:
    > stratus <> ´£¨ì:
    > : If I want to replace "-test" with "-test OK", and if it already contains "-test OK", it do not need to add it again.
    >
    > : Hence, String "-test" ========> will become ======> "-test OK"
    > : String "-test 1234 -test 456" =====> "-test OK1234 -test OK456"
    > : String "-test OK -test 45 -test OO" ====> "-test OK -testOK45 -test OKOO"
    >
    >
    > : How to do this?
    >
    >
    > I mean it should in a string.like
    > $string="-test -test 1234 -test 456 -test OK -test 45 -test OO";



    $ perl -le'
    my @x = ( "-test", "-test 1234 -test 456", "-test OK -test 45 -test OO" );
    for ( @x ) {
    print "Before: $_";
    s/-test\s*(?! ?OK)/-test OK/g;
    print " After: $_";
    }
    '
    Before: -test
    After: -test OK
    Before: -test 1234 -test 456
    After: -test OK1234 -test OK456
    Before: -test OK -test 45 -test OO
    After: -test OK -test OK45 -test OKOO



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 6, 2005
    #6
    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. afds

    Regular Exp

    afds, Oct 31, 2003, in forum: Python
    Replies:
    4
    Views:
    440
  2. Replies:
    9
    Views:
    382
    Paul McGuire
    Sep 7, 2009
  3. Jayan Jacob
    Replies:
    4
    Views:
    101
    Sandor Szücs
    Jul 15, 2008
  4. John Butler

    search reg-exp for exact match

    John Butler, Nov 20, 2008, in forum: Ruby
    Replies:
    2
    Views:
    222
    Robert Klemme
    Nov 22, 2008
  5. Merlin
    Replies:
    3
    Views:
    124
    Jeff 'japhy' Pinyan
    Jan 31, 2004
Loading...

Share This Page