Saju Palayur wrote:

> Hi
> 
> I am trying to do some thing like this, but am getting the wrong output.
> Can somebody tell if my regular expression is wrong or not?
> 
> ---------Perl script---------------------------------------
> $line = "ABCXYZGwcTI\\ABCXYZIntValTI";
> 
> $line=~s/ABCXYZ(.*)TI/Hello$1/g;
> print $line;
> ---------Output---------------------------------------
> HelloGwcTI\\ABCXYZIntVal

that's not the output of the above script. you shouldn't see '\\', there 
should be just one. the other one is long gone from the double quote. next 
time you post, you should be more careful to provide the correct output of 
your script as to avoid confusion.

> ---------Expected Output-------------------------
> HelloGwc\\HelloIntVal
> ----------------------------------------------------------

again, i assume you mean: "HelloGwc\HelloIntVal"

the reason is because (.*) is greedy and matching from the first 'G' up to 
the last 'l'. you need to stop the greedy-ness (.*) like:

#!/usr/bin/perl -w
use strict;

my $line1 = "ABCXYZGwcTI\\ABCXYZIntValTI";
my $line2 = $line1;

#--
#-- for your purpose, you can use either one of followings
#--
$line1 =~ s/ABCXYZ(.+?)TI/Hello$1/g;
$line2 =~ s/ABCXYZ(.*?)TI/Hello$1/g;

print $line1,"\n";
print $line2,"\n";

__END__

prints:

HelloGwc\HelloIntVal
HelloGwc\HelloIntVal

david
-- 
s,.*,<<,e,y,\n,,d,y,.s,10,,s
.ss.s.s...s.s....ss.....s.ss
s.sssss.sssss...s...s..s....
...s.ss..s.sss..ss.s....ss.s
s.sssss.s.ssss..ss.s....ss.s
..s..sss.sssss.ss.sss..ssss.
..sss....s.s....ss.s....ss.s

,....{4},"|?{*=}_'y!'+0!$&;"
,ge,y,!#:$_(-*[./<[EMAIL PROTECTED],b-t,
.y...,$~=q~=?,;^_#+?{~,,$~=~
y.!-&*-/:[EMAIL PROTECTED] ().;s,;,
);,g,s,s,$~s,g,y,y,%,,g,eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to