我從自己的正則表達式的猜測,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