$test=~ s/(dav)/$1 Smith/ig;
print "$test 
";

gives the following result:

dav Smithe Dav Smithid Dav Smithy
### 

$test=~ s/(dav)w+/$1 Smith/ig;
Gives us: dav Smith Dav Smith Dav Smith

The \w+ says "one or more word characters". Sticking that on the end gave us a bit
more control over the result by matching ONLY "dav" . Also notice we didn't need the
"$regex=" part, as you've already modified your string with the substitution. I'm
sure some of the big guns out there will be able to tighten this up, but my advice
is to be as specific as possible with regexes, and verbally go through them, i.e.
"the sring dav followed by one or more word characters," and so on.

As for the number of matches, it looks to me like the expression $regex=  ($test=~
s/(dav)/$1 Smith/ig); is actually forcing a scalar context on a list result (in
perl's eye), which is giving you the number. I'm curious to see if this is correct,
as I'm not 100% sure. Here's a small bit of back-up to my theory:

#!/usr/bin/perl -W

@test = qw (dave David Davy); # an array
$scalar=@test; # an array evaluated in scalar context
print "This is scalar context: $scalar 
"; 
##### output follows:

This is scalar context: 3

As always, critiques are welcome with any of my posts...
~Matt C.


--- David Gilden <[EMAIL PROTECTED]> wrote:
> Hello,
> Thanks for all the help this list is providing,
> 
> Here is today's problem:
> 
> 
> #!/usr/bin/perl
> 
> $test = "dave David Davy";
> 
> $i=0;
> 
> 
> ### Does not work.... want to count the number of matches...
> $regex=  ($test=~ s/(dav)/$i++ $1/eig);
> 
> print "$regex $i
";
> 
> ### This does work..
> $regex=  ($test=~ s/(dav)/$1 Smith/ig);
> 
> print "$regex
"; 
> 
> __END__
> 
> 
> It looks like  $regex contains the number of matches,
> what I wanted to was capture the modified string
> 
> Also can some provide an example of how to use the 'e' switch in
> a regrex,
> 
> Thanks
> 
> Dave
> ----------------
> 
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to