in reply to Regexp help
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]+):/;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regexp help
by toolic (Bishop) on Sep 03, 2010 at 16:48 UTC | |
|
Re^2: Regexp help
by Anonymous Monk on Sep 03, 2010 at 16:31 UTC |