When debugging something like this, it is often helpful to print out what is being matched. See below code.

I would suggest reading about "greedy" regexes. Perl regex'es are "greedy" meaning that they will match the maximal length thing while still allowing the rest of the regex to match. In this case, I "toned down" the \S+ pattern to match one and only one \S char by \S{1}. This could perhaps be \w{1} or \[a-zA-Z]{1}. Not sure what all variations that you are looking for.

Update: "\" was not needed for [a-zA-Z] (thanks! AnomalousMonk for spotting this typo!). The Perl \w,\s,\d or capitalized versions are so powerful that I seldom use a character set ..!

my $string_old = "<*2>H<2:0>,I<3:0>,<*2>P<4:0:2>"; my $string = "<*2>P<4:0:2>"; if(my@matches = ($string =~ /^<\*(\d+)>(\S{1})<(\d+):(\d+):(\d+)>$/)) { print "Matched ",join(" ", @matches),"\n"; } #printouts # orginal regex matched: # Matched 2 H<2:0>,I<3:0>,<*2>P 4 0 2 #new regex matches (old one did this too) #Matched 2 P 4 0 2
Update: I thought I'd add that this could have been just \S instead of \S{1}, but I wanted to show the general pattern for say \S{2} or whatever. \S{1,3} would be either 1,2 or 3 \S characters {min,max}.

In reply to Re: why does this match still? by Marshall
in thread why does this match still? by pip9ball

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.