Need regex to match 1 or more of exactly n-digit numbers
I need a regex to match a series of one or more n-digit numbers, separated by comma, ie:
abc12345def returns 12345
abc12345,23456def returns 12345,23456so far I got this: \d{5}(,\d{5})*
problem is it also matches in cases like these:
123456 returns 12345, but I need it not to match if the number is longer than 5. So I need numbers of exactly 5 digits, and if a number is shorter or longer开发者_运维技巧 it's a no-match
Thanks
Which language are you using for your regexes? You want to put non-digit markers around your \d{5}
's; here is the Perl syntax (with a negative look-ahead/look-behind fix by Lukasz):
(?<![\d,])\d{5}(,\d{5})*(?![\d,])
Actually I think I got it! (?<!\d)\d{5}(?!\d)(,(?<!\d)\d{5}(?!\d))*
I used the look-ahead and look-behind
Thanks.
You could use this one:
/\D?\d{5}(?:,\d{5})?\D?/
explanation:
/ : regex delimiter
\D? : non digit optionnal
\d{5} : 5 digits
(?: : begining of non-capture group
,\d{5} : comma and 5 digits
)? : end of group optionnal
\D? : non digit optionnal
/ : regex delimiter
精彩评论