[29980] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1223 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 22 11:14:17 2008

Date: Tue, 22 Jan 2008 08:14:11 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 22 Jan 2008     Volume: 11 Number: 1223

Today's topics:
        Variable interpolation and m in regular expression matc <josef.moellers@fujitsu-siemens.com>
    Re: Variable interpolation and m in regular expression  <noreply@gunnar.cc>
    Re: Variable interpolation and m in regular expression  <abigail@abigail.be>
    Re: Variable interpolation and m in regular expression  <jurgenex@hotmail.com>
    Re: Variable interpolation and m in regular expression  <josef.moellers@fujitsu-siemens.com>
    Re: Variable interpolation and m in regular expression  <abigail@abigail.be>
    Re: Variable interpolation and m in regular expression  <josef.moellers@fujitsu-siemens.com>
    Re: Variable interpolation and m in regular expression  <peter@makholm.net>
    Re: Variable interpolation and m in regular expression  <noreply@gunnar.cc>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 22 Jan 2008 14:42:30 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Variable interpolation and m in regular expression matching
Message-Id: <fn4rsd$oq9$1@nntp.fujitsu-siemens.com>

We were just discussing this and weren't able to resolve:

Imagine I have a variable $var and I'd like to use the m modifier on a=20
regular expression to match a line ending with "word" and the next=20
beginning with "var":

if (/word$var/m) ...

how does perl treat this: as "word" anywhere on a line followed by=20
whatever is in $var or as I described above?

Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html



------------------------------

Date: Tue, 22 Jan 2008 14:59:59 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <5vmb6vF1mm1bvU1@mid.individual.net>

Josef Moellers wrote:
> Imagine I have a variable $var and I'd like to use the m modifier on a 
> regular expression to match a line ending with "word" and the next 
> beginning with "var":
> 
> if (/word$var/m) ...
> 
> how does perl treat this: as "word" anywhere on a line followed by 
> whatever is in $var or as I described above?

What happened when you tried it?

Possibly is this what you are looking for:

     if ( /word\nvar/ )

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 22 Jan 2008 14:24:34 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <slrnfpbv52.1d4.abigail@alexandra.abigail.be>

                                                             _
Josef Moellers (josef.moellers@fujitsu-siemens.com) wrote on VCCLVII
September MCMXCIII in <URL:news:fn4rsd$oq9$1@nntp.fujitsu-siemens.com>:
==  We were just discussing this and weren't able to resolve:
==  
==  Imagine I have a variable $var and I'd like to use the m modifier on a 
==  regular expression to match a line ending with "word" and the next 
==  beginning with "var":
==  
==  if (/word$var/m) ...
==  
==  how does perl treat this: as "word" anywhere on a line followed by 
==  whatever is in $var or as I described above?


No, and not only because $var is interpolated. '$' will match a line *END*,
but not the end of line character.

For that, you need:

    if (/word\nvar/) 

no /m modifier needed. But you might want to use an /x modifier to
make it better readable:

    if (/word \n var/x)

And if you may have line endings from another system, you could use:

    if (/word \R var/x)


Abigail
-- 
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
 .qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
 .qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'


------------------------------

Date: Tue, 22 Jan 2008 14:47:43 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <6uvbp3hd51j26ojsmcuj2ndo6q7f1o4mpt@4ax.com>

Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote:
>We were just discussing this and weren't able to resolve:
>
>Imagine I have a variable $var and I'd like to use the m modifier on a 
>regular expression to match a line ending with "word" and the next 
>beginning with "var":
>
>if (/word$var/m) ...
>
>how does perl treat this: as "word" anywhere on a line followed by 
>whatever is in $var or as I described above?

I think there is quite some confusion about end-of-line, line end, the
dollar sign, and multi-line matches.

1: the dollar sign in a RE is only special if it is the last character in
the RE. In that case and only in that case it anchors(!) the RE to the end
of the string. It does not(!) match the newline character.
2: if you want to match a newline character then you will have to say so by
including the newline character in the RE: /word\n$var/
2: the m modifier allows an RE to expand across multiple lines within a
single string. However the given RE does not contain a \n or any wild card
that could match a \n. Therefore the m modifier is of no use in this RE.

So the RE matches the text 'word' followed by the content of $var
interpreted as a RE.

jue


------------------------------

Date: Tue, 22 Jan 2008 16:05:17 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <fn50ng$adl$1@nntp.fujitsu-siemens.com>

J=FCrgen Exner wrote:
> Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote:
>=20
>>We were just discussing this and weren't able to resolve:
>>
>>Imagine I have a variable $var and I'd like to use the m modifier on a =

>>regular expression to match a line ending with "word" and the next=20
>>beginning with "var":
>>
>>if (/word$var/m) ...
>>
>>how does perl treat this: as "word" anywhere on a line followed by=20
>>whatever is in $var or as I described above?
>=20
>=20
> I think there is quite some confusion about end-of-line, line end, the
> dollar sign, and multi-line matches.
>=20
> 1: the dollar sign in a RE is only special if it is the last character =
in
> the RE. In that case and only in that case it anchors(!) the RE to the =
end
> of the string. It does not(!) match the newline character.
> 2: if you want to match a newline character then you will have to say s=
o by
> including the newline character in the RE: /word\n$var/
> 2: the m modifier allows an RE to expand across multiple lines within a=

> single string. However the given RE does not contain a \n or any wild c=
ard
> that could match a \n. Therefore the m modifier is of no use in this RE=
=2E

Why then does "perldoc perlre" tell me this:

       sion inside are listed below.  Modifiers that alter the
        way a regular expression is used by Perl are detailed in
        "Regexp Quote-Like Operators" in perlop and "Gory details
        of parsing quoted constructs" in perlop.
=2E..
        m   Treat string as multiple lines.  That is, change "^"
            and "$" from matching the start or end of the string
            to matching the start or end of any line anywhere
            within the string.
=2E..
        does in any double-quoted string.)  The "\A" and "\Z" are
        just like "^" and "$", except that they won't match multi=AD
        ple times when the "/m" modifier is used, while "^" and
        "$" will match at every internal line boundary.  To match
        the actual end of the string and not ignore an optional
        trailing newline, use "\z".

You may be right in that a $ is only special at the end of the RE,=20
though, so I won't be able to match across line boundaries.

 > So the RE matches the text 'word' followed by the content of $var
 > interpreted as a RE.

Thanks,

Josef


--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html



------------------------------

Date: 22 Jan 2008 15:18:23 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <slrnfpc29v.4f6.abigail@alexandra.abigail.be>

                                                             _
Josef Moellers (josef.moellers@fujitsu-siemens.com) wrote on VCCLVII
September MCMXCIII in <URL:news:fn50ng$adl$1@nntp.fujitsu-siemens.com>:
**  
**  You may be right in that a $ is only special at the end of the RE, 
**  though, so I won't be able to match across line boundaries.


But it's not:

    $ perl -wE 'say "foo\nbar" =~ /o $ . b/smx'
    1
    $



Abigail
-- 
perl -wle 'print "Prime" if (0 x shift) !~ m 0^\0?$|^(\0\0+?)\1+$0'


------------------------------

Date: Tue, 22 Jan 2008 16:38:10 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <fn52l8$hnl$1@nntp.fujitsu-siemens.com>

Abigail wrote:
>                                                              _
> Josef Moellers (josef.moellers@fujitsu-siemens.com) wrote on VCCLVII
> September MCMXCIII in <URL:news:fn50ng$adl$1@nntp.fujitsu-siemens.com>:=


What a strange date ... would you care to explain? No, wait ... MCMXCIII =

is 1993 and ... what does the bar on top of the V mean? Ah, I see: If my =

date calculator is right, today would be the 5257th of September 1993,=20
so the V with the bar is actually MMMMM! Nice.
We have a colleague who always uses some date format from the French=20
Revolution (no, not the Spanish Inquisition, no-one expects the Spanish=20
Inquisition ;-): "D=E9cade I, Duodi de Pluvi=F4se de l'Ann=E9e 216 de la =

R=E9volution" (that was yesterday, btw).

> ** =20
> **  You may be right in that a $ is only special at the end of the RE, =

> **  though, so I won't be able to match across line boundaries.
>=20
>=20
> But it's not:
>=20
>     $ perl -wE 'say "foo\nbar" =3D~ /o $ . b/smx'
>     1
>     $

Iow: There could be a confusion, but there won't be because the two=20
cases will be distinct: I could never match the line-end followed=20
*immediately* by a word anyway.

Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html



------------------------------

Date: Tue, 22 Jan 2008 15:42:23 +0000
From: Peter Makholm <peter@makholm.net>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <87sl0pooio.fsf@hacking.dk>

Josef Moellers <josef.moellers@fujitsu-siemens.com> writes:

> Abigail wrote:
>>                                                              _
>> Josef Moellers (josef.moellers@fujitsu-siemens.com) wrote on VCCLVII
>> September MCMXCIII in <URL:news:fn50ng$adl$1@nntp.fujitsu-siemens.com>:
>
> What a strange date ... would you care to explain? No, wait
> ... MCMXCIII is 1993 and ... what does the bar on top of the V mean?
> Ah, I see: If my date calculator is right, today would be the 5257th
> of September 1993, so the V with the bar is actually MMMMM! Nice.

http://en.wikipedia.org/wiki/September_that_never_ended

//Makholm


------------------------------

Date: Tue, 22 Jan 2008 16:58:52 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Variable interpolation and m in regular expression matching
Message-Id: <5vmi5tF1nevbcU1@mid.individual.net>

Jürgen Exner wrote:
> 2: the m modifier allows an RE to expand across multiple lines within a
> single string.  However the given RE does not contain a \n or any wild card
> that could match a \n. Therefore the m modifier is of no use in this RE.

Why would you need things that match newlines in order for the /m 
modifier to make a difference?

C:\home>type test.pl
$_ = "alpha one\nbeta two\ngamma three\n";
@lastword = /(\w+)$/g;
@allnums = /(\w+)$/gm;
print "\@lastword: @lastword\n";
print "\@allnums:  @allnums\n";

C:\home>test.pl
@lastword: three
@allnums:  one two three

C:\home>

Neither do you need the /m modifier to make e.g. \s+ expand across 
multiple lines, do you?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1223
***************************************


home help back first fref pref prev next nref lref last post