You're really close. The problem is that \S+ matches anything besides whitespace, including colons and pipes. By default, + is greedy, so it matches as much as it can. So the first \S+ matches monsa::clear::1|red::23|blue::50|monsb::---quite a bit more than you want it to!
The two solutions are to not match colons or pipes with your regex, by using [^:|]+ instead of \S+, or to tell the regex to match non-greedily, by saying \S+? instead of \S+. I've done the latter here, and it seems to give the output that you want:
#!/usr/bin/perl $_="monsa::clear::1|red::23|blue::50|monsb::clear::80|red::90|blue::10 +0|"; @instances = m/(\S+?::\S+?::\S+?\|\S+?::\S+?\|\S+?::\S+?\b)/g; foreach $inst (@instances) { print "found $inst\n"; } exit;
In reply to Re: Regular Expression Trick
by sgifford
in thread Regular Expression Trick
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |