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.

In reply to Re: How does this regex not match? by 7stud
in thread How does this regex not match? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.