Sometimes, however, we'd like to specify where in the string the regexp should try to match. To do this, we would use the anchor metacharacters ^ and $ . The anchor ^ means match at the beginning of the string and the anchor $ means match at the end of the string, or before a newline at the end of the string.
You also misplaced your parentheses assuming you do not wish to capture leading and trailing :. You will get your expected result with
$str = "R4:abcxyz45:LNX"; $str =~ /^[\w\d]+:([\w\d]+):\w+$/;
or
$str = "R4:abcxyz45:LNX"; $str =~ /[\w\d]+:([\w\d]+):\w+/;
or even
depending on your needs.$str = "R4:abcxyz45:LNX"; $str =~ /:([\w\d]+):/;
In reply to Re: Regexp help
by kennethk
in thread Regexp help
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |