Bastien Bastien - 3 months ago 5x
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"


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


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


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


Here it is:


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:


You can test it here