For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Who can help me to explain the reason? about regex `m' modifier.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Who can help me to explain the reason? about regex `m' modifier.
Flw

2006-11-28, 6:59 pm

Who cabeginnersn help me to explain the reason?

$ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15, "\n";s/^a.*^b.*/x/m; print'
a11
b22
c33
---------------
a11
b22
c33
$ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15, "\n";s/^a.*\cJ^b.*/x/m; print'
a11
b22
c33
---------------
x
c33
$

flw
flw@perlchina.org
2006-11-28


Adriano Ferreira

2006-11-28, 6:59 pm

On 11/28/06, flw <flw@perlchina.org> wrote:
> Who cabeginnersn help me to explain the reason?
>
> $ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15, "\n";s/^a.*^b.*/x/m; print'


The problem here is that \m allows "^" to match after any newline
within the string, but does not change "." which matches any character
(except newline). The following can do what you want -- not pretty,
but works

$ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15,
"\n";s/^a(.|\n)*^b.*/x/m; print'
a11
b22
c33
---------------
x
c33

You may read about it in 'perldoc perlre' in the first four paragraphs
of the section "Regular Expressions" (perl 5.8.8).
Paul Lalli

2006-11-28, 6:59 pm

Adriano Ferreira wrote:
> On 11/28/06, flw <flw@perlchina.org> wrote:
>
> The problem here is that \m allows "^" to match after any newline
> within the string, but does not change "." which matches any character
> (except newline).


Correct. That behavior is controlled by the /s modifier.

> The following can do what you want -- not pretty,
> but works
>
> $ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15,
> "\n";s/^a(.|\n)*^b.*/x/m; print'


s/^a.*^b.*/x/ms;

Paul Lalli

Tom Phoenix

2006-11-28, 6:59 pm

On 11/27/06, flw <flw@perlchina.org> wrote:

> $ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15, "\n";s/^a.*^b.*/x/m; print'


$ perl -e '$_="a11\nb22\nc33\n"; print $_, "-"x15,
"\n";s/^a.*^b.*/x\n/ms; print'

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com