Brackets () in variable used for pattern match

Discussion in 'Perl Misc' started by Marcus Brody, Sep 3, 2003.

  1. Marcus Brody

    Marcus Brody Guest

    Hi,

    I'm new to perl and programming, so please please dont throw the
    manual at me!!

    Im trying to do a pattern match e.g.

    if $foo =~ m/$bar/

    However, on occasions $bar contains something like "HMGI(Y)", and the
    brackets are intefering with the pattern match. In all other
    instances my script works. I presume the brackets are being
    extrapolated such that my pattern match *essentially* becomes:

    if "HMGI(Y)" =~ m/HMGI(Y)/

    Which clearly wont work, as the brackets are only used to capture Y in
    the variable $1, and are otherwise "ignored". Is there a way to
    "escape" things in variables, even though I dont know whether they are
    going to contain a bracket or not??


    Thanks in advance


    MB
    Marcus Brody, Sep 3, 2003
    #1
    1. Advertising

  2. Marcus Brody

    Anno Siegel Guest

    Marcus Brody <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I'm new to perl and programming, so please please dont throw the
    > manual at me!!
    >
    > Im trying to do a pattern match e.g.
    >
    > if $foo =~ m/$bar/
    >
    > However, on occasions $bar contains something like "HMGI(Y)", and the
    > brackets are intefering with the pattern match. In all other
    > instances my script works. I presume the brackets are being
    > extrapolated such that my pattern match *essentially* becomes:
    >
    > if "HMGI(Y)" =~ m/HMGI(Y)/
    >
    > Which clearly wont work, as the brackets are only used to capture Y in
    > the variable $1, and are otherwise "ignored". Is there a way to
    > "escape" things in variables, even though I dont know whether they are
    > going to contain a bracket or not??


    Look up quotemeta(). Use \Q.

    Anno
    Anno Siegel, Sep 3, 2003
    #2
    1. Advertising

  3. Marcus Brody

    Thens Guest

    "William Hymen" <> wrote:

    >I just solved the problem yesterday for brackets. Funny you should ask.
    >I did this substitute before the match. It's not very elegant, but it works
    >by matching ANY character using the period in the same position
    >as the () charachters were.
    >
    > [ snipped a bad example ]
    >


    Please do not top post. Quote only what is needed and post your reply
    at the bottom of the quote.

    Back to the problem, your solution is not generic. When perl provides
    you facilities to Quote the meta characters in a regex, why reinvent the
    wheel that too badly. use \Q to quote.

    perl -e 'print "\QABCD(Y)"'

    to see what happens when you use \Q.

    Regards,
    Thens.
    Thens, Sep 3, 2003
    #3
  4. Marcus Brody

    Thens Guest

    On 3 Sep 2003 02:09:57 -0700
    (Marcus Brody) wrote:

    >Hi,
    >
    >I'm new to perl and programming, so please please dont throw the
    >manual at me!!


    You will remain a newbie, if you dont develop a liking for the manual
    ;-).

    Manual is the best source of reference, what more it is available in
    your hard disk. What are you waiting for.

    perldoc -f <fucntion>
    perldoc -q <keyword> # faq search
    perldoc perldoc # to know about the manual
    perldoc perldata # perl data structures
    perldoc perlre # Regular expressions
    perldoc perlref # references
    - more-

    Have fun !!

    Regards,
    Thens.
    Thens, Sep 3, 2003
    #4
  5. Marcus Brody

    Marcus Brody Guest

    >I just solved the problem yesterday for brackets. Funny you should ask.
    >I did this substitute before the match. It's not very elegant, but it works
    >by matching ANY character using the period in the same position
    >as the () charachters were.
    >
    >$save=$bar;
    >$save =~ s|\(|\.|g;
    >$save =~ s|\)|\.|g;
    >if $foo =~ m/$save/



    Hey

    Just came to pretty much the same conclusion myself ;-)
    instead, I quoted the offending characters rather than exchanging them:

    e.g.
    $save =~ s|\(|\\\(|g;
    etc.

    (with the "\\" equally an escaped "\" and the "\(" equalling an escaped "(" )

    This is pretty much the same as quotemeta, so I shall use that instead :)

    Thanks peoples

    MB
    Marcus Brody, Sep 3, 2003
    #5
  6. Marcus Brody

    John Bokma Guest

    William Hymen wrote:

    > Sorry, never knew there was a rule called top-posting.


    A rule *against*

    > I get tired of constantly scrolling to the bottom ;) - Bill


    We get tired of scrolling down to understand wtf a top poster is
    replying to. Also We are amazed at the garbage that hasn't been cut out.
    Cut pieces no longer needed and you don't have to scroll. And your
    replies will be much more understandable.

    BTW tinw

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
    John Bokma, Sep 5, 2003
    #6
  7. William Hymen <> wrote:

    > I get tired of constantly scrolling to the bottom ;) - Bill



    You do not need to scroll to the bottom when followups are
    composed properly.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 5, 2003
    #7
    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. hiwa
    Replies:
    0
    Views:
    637
  2. Victor
    Replies:
    2
    Views:
    644
    Victor
    May 17, 2004
  3. Sascha Bendix
    Replies:
    3
    Views:
    181
  4. Peter Stacy
    Replies:
    1
    Views:
    105
    John W. Krahn
    Nov 8, 2009
  5. User1014
    Replies:
    1
    Views:
    83
Loading...

Share This Page