Variable interpolation and m in regular expression matching

J

Josef Moellers

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?

Josef
 
G

Gunnar Hjalmarsson

Josef said:
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/ )
 
J

Jürgen Exner

Josef Moellers said:
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
 
J

Josef Moellers

Jürgen Exner said:
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.

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.
...
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.
...
does in any double-quoted string.) The "\A" and "\Z" are
just like "^" and "$", except that they won't match multi­
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,
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
 
J

Josef Moellers

Abigail said:
_
Josef Moellers ([email protected]) wrote on VCCLVII
September MCMXCIII in <URL:news:[email protected]>:

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.
We have a colleague who always uses some date format from the French
Revolution (no, not the Spanish Inquisition, no-one expects the Spanish
Inquisition ;-): "Décade I, Duodi de Pluviôse de l'Année 216 de la
Révolution" (that was yesterday, btw).
**
** 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
$

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

Josef
 
J

Jürgen Exner

Josef Moellers said:
Jürgen Exner wrote: [...]
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.
[...]
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.

Ooops! Somehow I must have gotten something badly mixed up.
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.

You may want to ignore this :-((

jue
 
F

Florian Kaufmann

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

Are you really sure about this rule? I think the $ anchor can also be
used right before the alternation operator | and before a closing
parenthesis ).

$ perl -e 'print "match:".(<> =~ /\d$|x/)."\n"' <<< 23f
match:
$ perl -e 'print "match:".(<> =~ /\d$|x/)."\n"' <<< 23
match:1

Flo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top