Sharan Basappa wrote:

> I have a script as follows  :
> 
> $str = "once upon a time
>         once upon a time";
> @store = $str =~ m/(once)/g;

That could be simplified to:

@store = $str =~ /once/g;

> print @store ;
> 
> This outputs "onceonce"
> How come regex is searching beyond newline. I thought the search will
> stop after first once.

A newline in a string is just a character like any other character, there is
nothing special about it.

> When I replace /g with /m, the output I get is "once", but I thought /m
> will tell regex at multiple lines for match.

/g says to match as many times as possible.  /m says that ^ will match at
the beginning of a line instead of at the beginning of a string and $ will
match at the end of a line instead of at the end of a string but you are
not using either ^ or $ in your pattern.

> Also when I replace /g with /s, I still get output "once"

/s says that the special character class . will match a newline which it
normally doesn't but you are not using . in your pattern.

> Can someone demystify this for me ?
> Is my assumption that regex will stop after encountering first newline is
> applicable only when dot* type of regex is used ?

$ perl -le'$_ = "abc\ndef\nghi\n"; print "*]${_}[*" for /.+/g'
*]abc[*
*]def[*
*]ghi[*
$ perl -le'$_ = "abc\ndef\nghi\n"; print "*]${_}[*" for /^.+/g'
*]abc[*
$ perl -le'$_ = "abc\ndef\nghi\n"; print "*]${_}[*" for /^.+/mg'
*]abc[*
*]def[*
*]ghi[*
$ perl -le'$_ = "abc\ndef\nghi\n"; print "*]${_}[*" for /^.+/sg'
*]abc
def
ghi
[*





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