Bastien Bastien - 5 months ago 13
Ruby Question

Regex ruby syntax to select a number while excluding specific ones

I am still struggling to find some ruby regex syntax despite the numerous documentation on-line. I have an array of string and I am looking for strings that include one number (whatever the number of digits) but not specific one (let's say for instance dates from 19XX to 201X).

I manage to get the regex for "the line contain a number"

.*\p{N}.*


I manage to get "exclude the line if this number is a year"

(?!19\d\d|20[0-1]\d)\d{4}


But I fail to combine both. I would need something that would intuitively be written as such

(.*\p{N}.*)&&(?!19\d\d|20[0-1]\d)\d{4}


But I am not sure how an AND operator can be used.

Answer

Here it is:

^(?!.*19\d\d.*)(?!.*20[01]\d.*)(.*\p{N}.*)$

You want a string that:

  • (?!.*19\d\d.*) doesn't contains 19xx
  • (?!.*20[01]\d.*) doesn't contains 200x or 201x
  • (.*\p{N}+.*) contains, at least, one digit

In regex && means, well, literal && and not and operator


If you want to capture numbers that are not in the range 1900-2019 you can replace with:

(?!\b19\d\d\b)(?!\b20[01]\d\b)(\b\p{N}+\b)

You can test it here