What does m@ stand for in the following regular expression.

G

grocery_stocker

Given

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);


What does m@ do in this case?
 
B

Bjoern Hoehrmann

* grocery_stocker wrote in comp.lang.perl.misc:
Given

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);

What does m@ do in this case?

The @ serves to delimit the regular expression, m@...@... is the same as
m/.../... except that different characters have to be escaped (using @
as delimiter, / does not have to be escaped inside the expression). m//
in turn is just the longhand for `/.../...`.
 
J

Jürgen Exner

grocery_stocker said:
Given

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);

What does m@ do in this case?

As a combination it doesn't do anything.

The m is the match operator (see perldoc perlop).
And the @ is the left delimiter of the following regular expression.

jue
 
T

Tim Greer

grocery_stocker said:
Given

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);


What does m@ do in this case?

m(anything) is making (anything) the delimiter. m// is just most common
to see, but m,, or m!! and so on, are perfectly acceptable. It can
help save typing over using m// when you have a lot of / characters
you'd otherwise have to backwack.

I.e.,

my ($title) = ($body =~
m!<title>\s*(.*?)\s*</title><b>this</b><i>that</i>!si);

saves typing over:

my ($title) = ($body =~
m@<title>\s*(.*?)\s*<\/title><b>this<\/b><i>that<\/i>@si);
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Tim Greer
m(anything) is making (anything) the delimiter. m// is just most common
to see, but m,, or m!! and so on, are perfectly acceptable.

It would be correct, except for

m(anything) is using `(' and `)' as delimiters.

;-) :-(

Ilya
 
T

Tim Greer

Ilya said:
[A complimentary Cc of this posting was sent to
Tim Greer
m(anything) is making (anything) the delimiter. m// is just most
common to see, but m,, or m!! and so on, are perfectly acceptable.

It would be correct, except for

m(anything) is using `(' and `)' as delimiters.

;-) :-(

Ilya

Haha. Okay, I really probably should have been more clear about the
example place holder. To the OP; Don't actually expect that to
work! :)
 
G

grocery_stocker

[A complimentary Cc of this posting was sent to
Tim Greer
m(anything) is making (anything) the delimiter. m// is just most common
to see, but m,, or m!! and so on, are perfectly acceptable.

It would be correct, except for

m(anything) is using `(' and `)' as delimiters.

And what would be the reason for using ( and ) as delimiters in this
case?
 
T

Tim Greer

grocery_stocker said:
[A complimentary Cc of this posting was sent to
Tim Greer
my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);
What does m@ do in this case?
m(anything) is making (anything) the delimiter. m// is just most
common to see, but m,, or m!! and so on, are perfectly acceptable.

It would be correct, except for

m(anything) is using `(' and `)' as delimiters.

And what would be the reason for using ( and ) as delimiters in this
case?

None, I had made a poor example of saying that the "delimiter character"
would be anything, by using the example of "(anything)" as the place
holder (for the example). That could cause confusion, so Ilya called
me on it. You _could_ use parenthesis if you wanted to m() instead of
m// or m@@ or m!! or m,, and so on. There's no reason for using any
one over another, other than personal preference, or in the case where
you can save typing and have it look cleaner, especially by not having
to backwack the same characters in the regex that you use as the
delimiter. I hope that's more clear (and I think Ilya called me on
this just for the above reason and the confusion it has already
caused).
 
M

Martien Verbruggen

grocery_stocker said:
[A complimentary Cc of this posting was sent to
Tim Greer
<[email protected]>:

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);
What does m@ do in this case?

m(anything) is making (anything) the delimiter. m// is just most
common to see, but m,, or m!! and so on, are perfectly acceptable.

It would be correct, except for

m(anything) is using `(' and `)' as delimiters.

And what would be the reason for using ( and ) as delimiters in this
case?

None, I had made a poor example of saying that the "delimiter character"
would be anything, by using the example of "(anything)" as the place
holder (for the example). That could cause confusion, so Ilya called
me on it. You _could_ use parenthesis if you wanted to m() instead of
m// or m@@ or m!! or m,, and so on.

One more footnote to this (and I'm sure you already know this, but
others might not):

If you use ? as the delimiter, then the pattern will only match once,
until you call reset().

Even if you don't ever need this, and even if you take into account that
that usage is deprecated, it's useful to know, as it can create some
weird and obscure bugs if you choose ?? as your delimiters, without
being aware of this behaviour.
There's no reason for using any
one over another, other than personal preference, or in the case where
you can save typing and have it look cleaner, especially by not having
to backwack the same characters in the regex that you use as the
delimiter.

Using brackets, either (), {}, <> or [] can be advantageous in that they
properly nest. What I mean by that is that you only ever have to escape
a closing bracket in the pattern if it hasn't been preceded by a opening
one. If you choose non-bracket delimiters, you need to escape your
delimiter in the pattern more often.

Also, most code editors have a 'find matching bracket' functionality
somewhere, which, for checking and debugging large regexen can be handy.

Check the section 'Quote and Quote-like Operators' in the perlop
documentation for more information.

Martien
 
T

Tim Greer

Martien said:
grocery_stocker said:
[A complimentary Cc of this posting was sent to
Tim Greer
<[email protected]>:

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);
What does m@ do in this case?

m(anything) is making (anything) the delimiter. m// is just most
common to see, but m,, or m!! and so on, are perfectly
acceptable.

It would be correct, except for

m(anything) is using `(' and `)' as delimiters.


And what would be the reason for using ( and ) as delimiters in this
case?

None, I had made a poor example of saying that the "delimiter
character" would be anything, by using the example of "(anything)" as
the place
holder (for the example). That could cause confusion, so Ilya called
me on it. You _could_ use parenthesis if you wanted to m() instead
of m// or m@@ or m!! or m,, and so on.

One more footnote to this (and I'm sure you already know this, but
others might not):

If you use ? as the delimiter, then the pattern will only match once,
until you call reset().

Even if you don't ever need this, and even if you take into account
that that usage is deprecated, it's useful to know, as it can create
some weird and obscure bugs if you choose ?? as your delimiters,
without being aware of this behaviour.
There's no reason for using any
one over another, other than personal preference, or in the case
where you can save typing and have it look cleaner, especially by not
having to backwack the same characters in the regex that you use as
the delimiter.

Using brackets, either (), {}, <> or [] can be advantageous in that
they properly nest. What I mean by that is that you only ever have to
escape a closing bracket in the pattern if it hasn't been preceded by
a opening one. If you choose non-bracket delimiters, you need to
escape your delimiter in the pattern more often.

Also, most code editors have a 'find matching bracket' functionality
somewhere, which, for checking and debugging large regexen can be
handy.

Check the section 'Quote and Quote-like Operators' in the perlop
documentation for more information.

Martien

Excellent points.
 
D

Dr.Ruud

Martien Verbruggen schreef:
If you use ? as the delimiter, then the pattern will only match once,
until you call reset().

And single quote is also special.
See perlop, Regexp Quote-Like Operators.
 
Q

QoS

grocery_stocker said:
Given

my ($title) = ($body =~ m@<title>\s*(.*?)\s*</title>@si);


What does m@ do in this case?

m = match
@ = marks the beginning and end of the regular expression
 
P

Petr Vileta \(fidokomik\)

m = match
@ = marks the beginning and end of the regular expression

For regular expresion you can use your own parentheses, so @ is parentheses in
this case. You can write it as

my ($title) = ($body =~ m{<title>\s*(.*?)\s*</title>}si);

or

my ($title) = ($body =~ m#<title>\s*(.*?)\s*</title>#si);

or

my ($title) = ($body =~ m/<title>\s*(.*?)\s*<\/title>/si);
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top