简体   繁体   English

需要正则表达式匹配1个或更多正好n位数字

[英]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: 我需要一个正则表达式来匹配一系列由逗号分隔的一个或多个n位数字,即:

abc12345def returns 12345 abc12345def返回12345
abc12345,23456def returns 12345,23456 abc12345,23456def返回12345,23456

so far I got this: \\d{5}(,\\d{5})* 到目前为止我得到了这个: \\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 123456返回12345,但如果数字长于5,我需要它不匹配。所以我需要5位数的数字,如果数字更短或更长,那就是不匹配

Thanks 谢谢

Which language are you using for your regexes? 你正在使用哪种语言的正则表达式? You want to put non-digit markers around your \\d{5} 's; 你想在\\d{5}周围放置非数字标记; here is the Perl syntax (with a negative look-ahead/look-behind fix by Lukasz): 这是Perl语法(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM