Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Matching brackets in Regular Expression

by Levan (Novice)
on Nov 21, 2003 at 01:14 UTC ( #308777=perlquestion: print w/replies, xml ) Need Help??

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


I am using a simple regular expression to match some text and the regular expression is something like this:

if ($text =~ /.*$stub.*/s) { #do something }

As you can see that I am using a scalar as a matching target, this is where the problem comes in

The problem is that this scalar $stub is volatile and it also contains a little bracket:

#volatile scalar $stub = 'bob(scalar)';

So how do I actually get the regular expression to work?? Thanks

Replies are listed 'Best First'.
Re: Matching brackets in Regular Expression
by thospel (Hermit) on Nov 21, 2003 at 06:45 UTC
    Never forget that regex matching is REGEX matching, NOT string matching. The difference becomes very important in cases like yours where the string contains characters that have a special meaning for regex. So what you actually need is to convert a given string to a regular expression that matches that string.

    In perl this is done using quotemeta, which you can also do using \Q...\E to make it happen on the enclosed string. You can drop the final \E if it coincides with the string end.

    The .* at the beginning and end are useless (even if you use $&, you can just use the whole string there instead supposing it's still unchanged). And once these are gone, the s modifier becomes useless too.

    So in the end you get:

    if ($text =~ /\Q$stub/) { # Do something }

    Or, since you are basically just looking for a substring, you can use index:

    if (index($text, $stub) >= 0) { # Do something }
      if (index($text, $stub) >= 0) { # Do something }
      was just what I was going to suggest. A regex may simply be too much of a tool. I've often wished I had a little electric typewriter for addressing envelopes, rather than going through monkeyshines trying to print envelopes and get the orientation, etc., correct. Simplest is best, sometimes.
Re: Matching brackets in Regular Expression
by Roger (Parson) on Nov 21, 2003 at 01:28 UTC
    You can use quotemeta function or \Q .. \E in your regular expression -
    if ($text =~ /.*\Q$stub\E.*/s) { #do something }
    Updated: I thought it might be useful for you to have a read of the perlre documentation on CPAN here. Look for quotemeta.

Re: Matching brackets in Regular Expression
by Hagbone (Monk) on Nov 21, 2003 at 02:21 UTC
    Using the quotemeta function is probably a good suggestion ...

    Is there any reason why you don't want to drop the


    from both ends of the regex ????

    My understanding is that using:

    if ($text =~ /$stub/s)
    will recognize any occurence of $stub

    Without knowing more about your intentions, it would seem difficult to offer detailed fine tuning advice.

Re: Matching brackets in Regular Expression
by RolandGunslinger (Curate) on Nov 21, 2003 at 15:15 UTC
    I don't understand...what makes the $stub scalar volatile?
      The problem is that this scalar $stub is volatile and it also contains a little bracket...

      I believe that though he listed that bob(scalar), it should be bob[scalar] as the description lists. In this case it would be interpreted as a character class instead of a string. \Q and \E would backslash these brackets.

      In it's current form, there's really no conflict unless the string is something like bob(??{print $hi}).

      Who is Kayser Söze?
      All code is untested unless I say so.

        Except that the parens () will be used for grouping and capturing by the regex, and so won't match the literals in the string;

        perl -e '$s = q|b(s)|; print "without quotemeta" if $s =~ /$s/; print + "with quotemeta" if $s =~ /\Q$s\E/;' with quotemeta


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://308777]
Approved by Roger
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-09-29 07:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found