Regex to find numbers excluding four digit numbers

余生长醉 提交于 2019-12-07 05:03:24

If lookbehind and lookahead are available, the following should work:

(?<!\d)(\d{1,3}|\d{5,})(?!\d)

Explanation:

(?<!\d)            # Previous character is not a digit
(\d{1,3}|\d{5,})   # Between 1 and 3, or 5 or more digits, place in group 1
(?!\d)             # Next character is not a digit

If you cannot use lookarounds, the following should work:

\b(\d{1,3}|\d{5,})\b

Explanation:

\b                 # Word boundary
(\d{1,3}|\d{5,})   # Between 1 and 3, or 5 or more digits, place in group 1
\b                 # Word boundary

Python example:

>>> regex = re.compile(r'(?<!\d)(\d{1,3}|\d{5,})(?!\d)')
>>> regex.findall('1 22 333 4444 55555 1234 56789')
['1', '22', '333', '55555', '56789']

Depending on the regex flavor you use, this might work for you:

(([0-9]{1,3})|([0-9]{5,}))

(\\d{0,4} | \\d{6,}) in java.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!