Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How does this regex not match?

by 7stud (Deacon)
on Feb 13, 2010 at 11:54 UTC ( [id://823007]=note: print w/replies, xml ) Need Help??


in reply to How does this regex not match?

If you are having trouble figuring out what the previous responses are saying, the regex my_stuff[1] is equivalent to the regex my_stuff1, which does not appear in your original string.

The regex my_stuff[1] says to look for the string 'my_stuff' followed by one of the characters specified in the brackets. Because you only have one character specified in the brackets, the regex is equivalent to mystuff1.

Here is an example of how brackets can prove useful in a regex:

use strict; use warnings; use 5.010; my $pattern = 'x[ABC]'; my @strings = ( 'xA', 'xB', 'xC', 'xY', ); for (@strings) { if (/$pattern/) { say; } } --output:-- xA xB xC

In order to literally match any of the regex characters that have special meaning (e.g. * . ?), you have to 'escape' the character. You 'escape' a special character with a '\', which tells perl to ignore the special regex meaning of the character. As it turns out, you only have to escape the opening bracket, and then perl knows the closing bracket is to be interpreted as a literal bracket:

use strict; use warnings; use 5.010; my $string= 'my_stuff[1]=400'; my $pattern = 'my_stuff\[1]='; say "They match." if $string =~ /$pattern/; --output:-- They match.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://823007]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2026-02-07 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.