How to replace globally a string1 with string2 only if string1 does not have a sub_strig

Discussion in 'Perl Misc' started by giri alamuri, Sep 5, 2003.

  1. giri alamuri

    giri alamuri Guest

    What I want to do is:

    s/begin(^(abc)).*end//xyz/gs

    What I meant above is, find string begin which is not followed by abc and
    then followed by any string till it find "end" and replace the whole string
    with "xyz" globally.

    That is, I want to replace begin.*end with xyz only if begin is not followed
    by "abc".

    I want to do it globally. So, following will not work:

    unless (/beginabc/) {
    s/begin(^(abc)).*end//xyz/gs
    }
    giri alamuri, Sep 5, 2003
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Fri, 05 Sep 2003 13:24:46 -0700, giri alamuri wrote:

    > What I want to do is:


    Go ahead, we're not going to stop you.

    > s/begin(^(abc)).*end//xyz/gs
    >
    > What I meant above is, find string begin which is not followed by abc and
    > then followed by any string till it find "end" and replace the whole string
    > with "xyz" globally.
    >
    > That is, I want to replace begin.*end with xyz only if begin is not followed
    > by "abc".
    >
    > I want to do it globally. So, following will not work:
    >
    > unless (/beginabc/) {
    > s/begin(^(abc)).*end//xyz/gs
    > }


    Try a negative look ahead.

    Also, when asking a question in english, the sentence should end with a
    question mark (?).

    - Brian
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/WP1aiK/rA3tCpFYRAvtEAJ9X6FNtOuXIgrOHCvXH3SC6BgrEEwCgjrqz
    CgXyJBYJrRcmk0Y1ckEXHgM=
    =MNE9
    -----END PGP SIGNATURE-----
    Brian Harnish, Sep 5, 2003
    #2
    1. Advertising

  3. giri alamuri

    giri alamuri Guest

    I tried negative look ahead, but it does not work.
    Having (?!...) does not have any effect. The ... string
    is matched as normal string.

    Am I doing any mistake in using negative look ahead?

    Here is the sample code and output:

    #!/usr/lecal/bin/perl -w

    foreach (<DATA>) {
    s/abc($!_do_not_match_)def/uvwxyz/g;
    print;
    }

    __DATA__
    abc_do_not_match_def
    abc_do_not_matchmatch_def
    abcdef

    script output:
    ---------------
    uvwxyz
    abc_do_not_matchmatch_def
    abcdef

    Here, the first line should not have matched - it did.
    second line did not match as expected.
    third line should have matched - it did not.

    It is as if (?!...) does not have any effect.
    As a side note, it does has a side effect. It does not
    set $1, which I was expecting the behavior of ($:)


    I am using perl V5.60

    Please answer.
    Thanks,
    Giri



    Brian Harnish <> wrote in message news:<>...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > On Fri, 05 Sep 2003 13:24:46 -0700, giri alamuri wrote:
    >
    > > What I want to do is:

    >
    > Go ahead, we're not going to stop you.
    >
    > > s/begin(^(abc)).*end//xyz/gs
    > >
    > > What I meant above is, find string begin which is not followed by abc and
    > > then followed by any string till it find "end" and replace the whole string
    > > with "xyz" globally.
    > >
    > > That is, I want to replace begin.*end with xyz only if begin is not followed
    > > by "abc".
    > >
    > > I want to do it globally. So, following will not work:
    > >
    > > unless (/beginabc/) {
    > > s/begin(^(abc)).*end//xyz/gs
    > > }

    >
    > Try a negative look ahead.
    >
    > Also, when asking a question in english, the sentence should end with a
    > question mark (?).
    >
    > - Brian
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.2 (GNU/Linux)
    >
    > iD8DBQE/WP1aiK/rA3tCpFYRAvtEAJ9X6FNtOuXIgrOHCvXH3SC6BgrEEwCgjrqz
    > CgXyJBYJrRcmk0Y1ckEXHgM=
    > =MNE9
    > -----END PGP SIGNATURE-----
    giri alamuri, Sep 6, 2003
    #3
  4. giri alamuri <> wrote:

    > Having (?!...) does not have any effect.

    ^^^

    > s/abc($!_do_not_match_)def/uvwxyz/g;

    ^^^


    One of these things is not like the other, one of these things
    just isn't the same...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 6, 2003
    #4
  5. giri alamuri <> wrote:

    > Here is the sample code and output:
    >
    > #!/usr/lecal/bin/perl -w

    ^
    ^

    Do not re-type Perl code

    Use copy/paste or your editor's "import" function rather than
    attempting to type in your code. If you make a typo you will get
    followups about your typos instead of about the question you are
    trying to get answered.


    Like this one.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 6, 2003
    #5
  6. giri alamuri

    giri alamuri Guest

    Hello Ryan and Brian,
    Thanks for your help.
    Negative look ahead worked for me.

    Regards,
    Giri

    Ryan Shondell <-state.edu> wrote in message news:<-state.edu>...
    > (giri alamuri) writes:
    >
    > > What I want to do is:
    > >
    > > s/begin(^(abc)).*end//xyz/gs
    > >
    > > What I meant above is, find string begin which is not followed by abc and
    > > then followed by any string till it find "end" and replace the whole string
    > > with "xyz" globally.
    > >
    > > That is, I want to replace begin.*end with xyz only if begin is not followed
    > > by "abc".

    >
    > I think this is a job for the negative look-ahead assertion, with some
    > non-greediness added...
    >
    > s/begin(?!abc).*?end/xyz/g;
    >
    >
    > Ryan
    giri alamuri, Sep 12, 2003
    #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. Steve Harrad
    Replies:
    5
    Views:
    4,490
    Mike Treseler
    Dec 22, 2003
  2. james
    Replies:
    1
    Views:
    280
    Eric Sosman
    Mar 7, 2008
  3. asil klin
    Replies:
    28
    Views:
    1,279
    Andreas Leitgeb
    Mar 5, 2011
  4. V S Rawat
    Replies:
    5
    Views:
    286
    Richard Cornford
    Jul 3, 2007
  5. Rudra Banerjee
    Replies:
    5
    Views:
    297
    Kaz Kylheku
    Sep 15, 2012
Loading...

Share This Page