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

G

giri alamuri

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

Brian Harnish

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

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

giri alamuri

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
 
T

Tad McClellan

giri alamuri said:
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...
 
T

Tad McClellan

giri alamuri said:
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.
 
G

giri alamuri

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

Regards,
Giri
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top