负向前瞻性正则表达式直到数字但没有连续的0

时间:2014-03-19 09:01:53

标签: java regex

我正在提取一系列3-6个数字的字符串。但是我不想包含一个连续超过3个0的数字。

我现在所掌握的是一个常规的前瞻,但我如何实现零部分呢?

(\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

1 个答案:

答案 0 :(得分: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