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 ..!
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}.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
In reply to Re: why does this match still?
by Marshall
in thread why does this match still?
by pip9ball
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |