2014-03-19 31 views
3

我正在提取一個字符串到一系列3-6數字。不過,我不想包含超過3個連續0的數字。負向前瞻正則表達式直到數字,但沒有連續0s

我現在擁有的是一個經常性的前瞻,但是我如何實現零部件?

(\d{3,6})[:|\s]{0,2}([a-zA-Z]{3})((?:(?!\d{3,6}).)*) 

例輸入:

010113 tee Some text for a 1000 reasons 020113 mee More text 

所以輸入的格式爲[3-6 numbers] [3 letter identifier] [message](反覆)

我需要它的字符串匹配起來,直到020113,不只是直到1000

+0

含有多於* 6位數字符的字符串應該是什麼結果?也忽略它? –

+0

據此編輯。 – Difusio

+0

該字符串已經以6位數字開頭,爲什麼不在這些之前停止? –

回答

3

可以嵌套模式斷言:

((?:(?!(?!\d*000)\d{3,6}).)*) 

說明:

(   # Match/capture in group 1: 
(?:   # Start of non-capturing group. 
    (?!   # Assert that it's impossible to match... 
    (?!\d*000) # (unless it's a number that contains 000) 
    \d{3,6} # a number of three to six digits here. 
)   # End of lookahead 
    .   # Match any character 
)*   # End of non-capturing group, repeat any number of times 
)    # End of capturing group 1 

看到它live on regex101.com

+0

輝煌,感謝您的詳細解釋和指引我到那個有用的網站。 – Difusio