我必须编写一个检测6位或9位的正则表达式--意思是:
ID 123 - it should not meet the regext as it is 3 digits
ID 1234 - it should not meet the regext as it is 4 digits
ID 12345 - it should not meet the regext as it is 5 digits
ID 123456 - it should meet the regex it is 6 digits <==========
ID 1234567 - it should not meet the regext as it is 7 digits
ID 12345678 - it should not meet the regext as it is 8 digits
ID 123456789 - it should meet the regext as it is 9 digits <==========
ID 1234567890 - it should not meet the regext as it is 10 digits
ID 12345678901 - it should not meet the regext as it is 11 digits以此类推。你想明白了吗?
有没有人能帮我做一下这个正则表达式?
发布于 2019-06-12 23:23:19
您可以尝试使用此模式:
\b(\d{6}|\d{9})\b发布于 2019-06-12 23:47:00
或者另一个:
\b\d{6}(?:\d{3})?\b\b与word boundary\d{6}匹配六位数(\d是digit)(?:\d{3})?的short包含另外三位数的optional non capturing group 发布于 2019-06-13 01:04:45
如果要匹配ID部分,则可以使用替换来匹配6位或9位数字。
使用锚点断言字符串的起始^和结束$,或者将单词边界\b添加到模式的开头和结尾。
数字在第一个捕获组中。
^ID (\d{6}|\d{9})$https://stackoverflow.com/questions/56565553
复制相似问题