Just use negative look-arounds to ensure that you do not have a digit on either side of a 3 or 4 digit number:
my @strings = ( 'foo1234bar', '1234 5678', 'abcd9012f123ab', '123', ' 123', '123 ', ); foreach my $string (@strings) { my(@nums) = $string =~ m/(?<!\d)(\d{3,4})(?!\d)/g; local $" = ','; print "<$string>: (@nums)\n"; }
The output:
<foo1234bar>: (1234) <1234 5678>: (1234,5678) <abcd9012f123ab>: (9012,123) <123>: (123) < 123>: (123) <123 >: (123)
An advantage of using negative lookarounds is that you don't have to explicitly accommodate conditions such as the start or end of string or line. The negative lookarounds are just saying "a digit cannot come immediately before or after a sequence of 3 or 4 digits". With positive lookarounds you would have to say "either a non-digit or end of string must come before and after a sequence of 3 or 4 digits." That would look something like this (untested):
m/(?<^|\D)(\d{3,4})(?=$|\D)/mgSo rather than asserting what must come before and after the digits, the regexp becomes simpler if we just assert what cannot come before or after.
Dave
In reply to Re: Recognizing 3 and 4 digit number
by davido
in thread Recognizing 3 and 4 digit number
by htmanning
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |