Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Multiple Match of regular exp...

Reply
Thread Tools

Multiple Match of regular exp...

 
 
stratus
Guest
Posts: n/a
 
      05-06-2005
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?



 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      05-06-2005
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
 
Reply With Quote
 
 
 
 
stratus
Guest
Posts: n/a
 
      05-06-2005
stratus <(E-Mail Removed)> :
: 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";
 
Reply With Quote
 
Damian James
Guest
Posts: n/a
 
      05-06-2005
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:

?!
 
Reply With Quote
 
Damian James
Guest
Posts: n/a
 
      05-06-2005
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:

?!
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-06-2005
stratus wrote:
> stratus <(E-Mail Removed)> :
> : 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expressions and multiple match ast Javascript 4 03-11-2011 04:31 PM
regular expressions and multiple match ast Javascript 2 03-11-2011 10:50 AM
Regular expressions (multiple match problem) mikko.n C Programming 5 04-02-2008 10:14 PM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments