Professional Web Applications Themes

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

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 }...

  1. #1

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

    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 Guest

  2. #2

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

    -----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 Guest

  3. #3

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

    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 <bharnishtechnologist.com> wrote in message news:<pan.2003.09.05.21.17.19.983253technologist. com>...
    > -----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 Guest

  4. #4

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

    giri alamuri <galamuri81> 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
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #5

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

    giri alamuri <galamuri81> 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
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  6. #6

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

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

    Regards,
    Giri

    Ryan Shondell <shondellcis.ohio-state.edu> wrote in message news:<xcwu17rge59.fsfpsi.cis.ohio-state.edu>...
    > [email]galamuri81[/email] (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 Guest

Similar Threads

  1. Links - globally applying to a multi-page doent?
    By hungry_ghost@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 2
    Last Post: November 11th, 03:13 PM
  2. Search and replace (super global replace)
    By johnweiffenbach@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 1
    Last Post: April 8th, 08:56 AM
  3. Globally change fontsize in website
    By A.M in forum Macromedia Dynamic HTML
    Replies: 9
    Last Post: January 11th, 03:45 PM
  4. storing values non-globally
    By Kevin Pfeiffer in forum PERL Beginners
    Replies: 1
    Last Post: October 5th, 11:26 PM
  5. newbie needs to globally edit his site
    By leegold in forum PERL Miscellaneous
    Replies: 4
    Last Post: July 2nd, 08:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139