advait has asked for the wisdom of the Perl Monks concerning the following question:

Please help ....with the code below
my $ipn="( going ) there"; my $jpn="( going ) there"; if($jpn =~ m/$ipn/) { print "yes"; } else {print "nomatch";}
thank you in advance
  • Comment on how can I use regular expresion if my string to be matched has round brackets
  • Download Code

Replies are listed 'Best First'.
Re: how can I use regular expresion if my string to be matched has round brackets
by Fletch (Bishop) on Oct 06, 2008 at 15:09 UTC

    See quotemeta (and/or the docs for \Q in (I believe) perlop's discussion on quotelikes), and read perlretut.

    Update: Added parenthetical about \Q.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: how can I use regular expresion if my string to be matched has round brackets
by toolic (Bishop) on Oct 06, 2008 at 15:10 UTC
Re: how can I use regular expresion if my string to be matched has round brackets
by jettero (Monsignor) on Oct 06, 2008 at 15:14 UTC
    Although the quotemeta and \Q\E answers are correct, you can also use \(\) like so:
    if( m/(\()/ ) { print "I matched a paren: $1\n"; }
    In fact, quotemeta and \Q\E will actually transform a string usefully (so you can see what escapes to use for next time):
    print "\Qhrm, do I have to escape dots (.) and question marks?\E\n";
    UPDATE: I thought quotemeta was smarter than s/(\W)/\\$1/g. count me underwhelmed. I did wonder why it was escaping spaces.

    -Paul

      In fact, quote meta and \Q\E will actually transform a string usefully (so you can see what escapes to use for next time):

      Well, quotemeta and \Q will simple escape any character that isn't a letter, digit or underscore. It doesn't have any knowledge of which characters are special and which aren't, except for the promise that if character that isn't a letter, digit or underscore is escaped, it will never carry a special meaning inside a regexp. I'm not quite sure what you mean by "useful" and "see what escapes to use for next time", but people often frown when commas, spaces and other 'harmless' punctuation characters are escaped in a regexp literal.

        quotemeta is both defined by s/(\W)/\\$1/ and by "being the function with which you escape strings in regexes", which means that in perl 5, a sequence of a backslash and a non-word-character will always literally match the non-word characters (because otherwise it would break too much existing code).

        Sadly also no non-word-character can get a new meaning without a preceding backslash, because it would also break stuff.

        So there remain only a few possible ways in which new features can be introduced syntactically:

        • With modifiers that change the syntax
        • By using one of the rare free escape sequences (\F, \T, \y, \Y)
        • By constructs that are now syntactically not valid, because they don't make any sense (perhaps ({...}), previously (* ... ))

        All of these don't quite help to clean up the syntax, though.

      thank you all :)
Re: how can I use regular expresion if my string to be matched has round brackets
by Grey Fox (Chaplain) on Oct 06, 2008 at 15:21 UTC
    Don't forget to use a \ to escape out special characters when using regex.
    #!/pw/prod/svr4/bin/perl use warnings; use strict; use Data::Dumper; my $ipn= qr/\( going \) there/; my $jpn="( going ) there"; if($jpn =~ m/$ipn/) { print "yes"; } else {print "nomatch";}
    Lots of good regex info if you search around Perl Monks try Perlmonks Tutorial: Regular Expressions Tutorial, the Basics (for BEGINNERS)
    I have also found http://www.regular-expressions.info/quickstart.html very helpful working with regex's. Lots of good examples.
    I found tools like http://www.txt2re.com/ or Eclipses regex tool to be helpful in debugging my regex commands.
    Good Luck.

    -- Grey Fox
    "We are grey. We stand between the darkness and the light" B5
      why it works with
      my $ipn=qr/\( going \) there/; or my $ipn=qw/\( going \) there/;
      but it doesn't work with
      my $ipn="\( going \) there";
      Thanks.

        You need:

        my $ipn="\\( going \\) there";

        The \\ gets replaced by \ when the string is parsed.


        Perl reduces RSI - it saves typing