Use a negative zero-width lookahead assertion:
perl -E '$x="test: 111\ntest: 222 test: 333"; $x=~/test:\s+(\d{3})(?!t +est:\s+\d{3})$/s; say $1'; 333
What the
/test:\s+(\d{3})(?!test:\s+\d{3})$/sregex does is looks for test:, and captures the following three digits, so long as there's not another test: NNN anywhere else after it (meaning the last one in the string). The /s modifier allows you to search across newlines. I've used \s+ instead of a literal space just to ensure that it'll match any type of whitespace (tab, multiple consecutive spaces etc).
In reply to Re: Match only last occurrence
by stevieb
in thread Match only last occurrence
by sotona
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |