Regex find numbers excluding four-digit numbers

I am trying to figure out how to find numbers that are not years (I define the year as just a four-digit number).

For example, I want to select

1 12 123 

But NOT 1234 to avoid dates (4 digits).

if the regular expression also chose 12345 , which is fine, but not necessary to solve this problem

(Note: these requirements may seem strange. They are part of a larger solution for which I am stuck)

+1
source share
3 answers

If lookbehind and lookahead are available, then 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 search terms, follow these steps:

 \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'] 
+5
source

Depending on your regular expression effect, this might work for you:

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

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

-one
source

All Articles