我从自己的正则表达式的猜测,5和8之间允许在一行中没有空格的数字。如果这是真的,那么下面的正则表达式可能会做到这一点(用Python编写的例子)。它允许单个数字组的长度在5到8位之间。如果有多个组,则允许每个组具有正好3个数字,除了最后一个组可以是1到3个数字之间。左侧的一个加号是可选的。
你解析电话号码吗?:)
In [176]: regex = re.compile(r"""
^ # start of string
(?: \+\s)? # optional plus sign followed by whitespace
(?:
(?: \d{3}\s)+ # one or more groups of three digits followed by whitespace
\d{1,3} # one group of between one and three digits
| # ALTERNATIVE
\d{5,8} # one group of between five and eight digits
)
$ # end of string
""", flags=re.X)
# --- MATCHES ---
In [177]: regex.findall('123 456 7')
Out[177]: ['123 456 7']
In [178]: regex.findall('12345')
Out[178]: ['12345']
In [179]: regex.findall('+ 123 456 78')
Out[179]: ['+ 123 456 78']
In [200]: regex.findall('12345678')
Out[200]: ['12345678']
# --- NON-MATCHES ---
In [180]: regex.findall('123456789')
Out[180]: []
In [181]: regex.findall('+ 124 578a')
Out[181]: []
In [182]: regex.findall('+123456789')
Out[182]: []
In [198]: regex.findall('123')
Out[198]: []
In [24]: regex.findall('1234 556')
Out[24]: []
你为什么不分两步做?首先检查它是否只包含所需的字符,然后对数字进行计数。 –
我使用的服务只有一个字段的正则表达式条件。 :( – mannge