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

Discussion in 'Perl Misc' started by grocery_stocker, Sep 27, 2008.

  1. Given

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


    What does m@ do in this case?
     
    grocery_stocker, Sep 27, 2008
    #1
    1. Advertising

  2. * 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 `/.../...`.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
     
    Bjoern Hoehrmann, Sep 27, 2008
    #2
    1. Advertising

  3. grocery_stocker <> wrote:
    >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
     
    Jürgen Exner, Sep 27, 2008
    #3
  4. grocery_stocker

    Tim Greer Guest

    grocery_stocker wrote:

    > 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);


    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Sep 27, 2008
    #4
  5. [A complimentary Cc of this posting was sent to
    Tim Greer
    <>], who wrote in article <JfvDk.29$>:
    > > 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.

    ;-) :-(

    Ilya
     
    Ilya Zakharevich, Sep 27, 2008
    #5
  6. grocery_stocker

    Tim Greer Guest

    Ilya Zakharevich wrote:

    > [A complimentary Cc of this posting was sent to
    > Tim Greer
    > <>], who wrote in article
    > <JfvDk.29$>:
    >> > 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.
    >
    > ;-) :-(
    >
    > 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! :)
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Sep 27, 2008
    #6
  7. On Sep 27, 1:16 pm, Ilya Zakharevich <> wrote:
    > [A complimentary Cc of this posting was sent to
    > Tim Greer
    > <>], who wrote in article <JfvDk.29$>:
    >
    > > > 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?
     
    grocery_stocker, Sep 27, 2008
    #7
  8. grocery_stocker

    Tim Greer Guest

    grocery_stocker wrote:

    > On Sep 27, 1:16 pm, Ilya Zakharevich <> wrote:
    >> [A complimentary Cc of this posting was sent to
    >> Tim Greer
    >> <>], who wrote in article
    >> <JfvDk.29$>:
    >>
    >> > > 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).
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Sep 27, 2008
    #8
  9. On Sat, 27 Sep 2008 13:41:20 -0700,
    Tim Greer <> wrote:
    > grocery_stocker wrote:
    >
    >> On Sep 27, 1:16 pm, Ilya Zakharevich <> wrote:
    >>> [A complimentary Cc of this posting was sent to
    >>> Tim Greer
    >>> <>], who wrote in article
    >>> <JfvDk.29$>:
    >>>
    >>> > > 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
    --
    |
    Martien Verbruggen | "In a world without fences,
    | who needs Gates?"
    |
     
    Martien Verbruggen, Sep 27, 2008
    #9
  10. grocery_stocker

    Tim Greer Guest

    Martien Verbruggen wrote:

    > On Sat, 27 Sep 2008 13:41:20 -0700,
    > Tim Greer <> wrote:
    >> grocery_stocker wrote:
    >>
    >>> On Sep 27, 1:16 pm, Ilya Zakharevich <> wrote:
    >>>> [A complimentary Cc of this posting was sent to
    >>>> Tim Greer
    >>>> <>], who wrote in article
    >>>> <JfvDk.29$>:
    >>>>
    >>>> > > 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.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Sep 27, 2008
    #10
  11. grocery_stocker

    Dr.Ruud Guest

    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.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 28, 2008
    #11
  12. grocery_stocker

    Guest

    grocery_stocker <> wrote in message-id: <>

    >
    > 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
     
    , Oct 2, 2008
    #12
  13. d wrote:
    > grocery_stocker <> wrote in message-id:
    > <>
    >
    >>
    >> 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


    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);


    --
    Petr Vileta, Czech republic
    (My server rejects all messages from Yahoo and Hotmail.
    Send me your mail from another non-spammer site please.)
    Please reply to <petr AT practisoft DOT cz>
     
    Petr Vileta \(fidokomik\), Oct 6, 2008
    #13
  14. Michael Paoli, Oct 6, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,334
  2. Edward Yang

    What does ~ stand for in ImageUrl?

    Edward Yang, Jul 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    368
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 22, 2004
  3. Replies:
    8
    Views:
    6,622
    David Dorward
    Nov 22, 2005
  4. Plotinus
    Replies:
    2
    Views:
    304
    Tad McClellan
    Dec 17, 2004
  5. kun niu
    Replies:
    14
    Views:
    191
Loading...

Share This Page