Multiple Match of regular exp...

S

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?
 
J

John W. Krahn

stratus said:
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
 
S

stratus

stratus <[email protected]> ´£¨ì:
: 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";
 
D

Damian James

$ 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:

?!
 
D

Damian James

$ 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:

?!
 
J

John W. Krahn

stratus said:
stratus <[email protected]> ´£¨ì:
: 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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top