Jennifer Foo wrote:
> Someone posted this regex question which I can't understand for.
> 
> perl -e '$_=abc.eeeee.i;   
> s/(\.\w+)?$/.out/;
> print;'
> 
> the result is: abceeeeei.out
> 
> Why is this?Please help explain it.Thanks!

$_=abc.eeeee.i;

This is short for:

$_ = 'abc' . 'eeeee' . 'i';

Which is the same as saying:

$_ = 'abceeeeei';


The substitution:

s/(\.\w+)?$/.out/;

Says to match a period (.) followed by one or more 'word' characters (\w+).
This pattern is enclosed in parentheses and this group should match zero or
one time.  The pattern is anchored to the end of the string.  Because the
pattern will always match, the replacement string '.out' will either be
appended to the string or replace any (\.\w+) pattern at the end of the string.



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

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


Reply via email to