0

I want to select all lines which do not start with the following the format:

[0-9]{3}[a-zA-Z]{3}[0-9]{4}

ex. of valid ID

123-ABC-4567, 15 W. 15th St., 50.1

ex. of invalid ID

Q2Z-457-QWER, 200 East 20th Street, 49
678-FGH-9845 ,,45 5th Ave, 12.2,

I have tried doing something along the lines of :

(?!([\s+]*[0-9]{3}[-][a-zA-Z]{3}[-][0-9]{4}[\s+]*[,][\s+]*[0-9]{2,3}[\s+]*))

However, it seems select other things in addition to the invalid strings

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • 1
    You could use a negative lookahead, but since you tagged java I strongly advise you simply negate the boolean `Matcher.find()` returns – Aaron Feb 06 '19 at 19:43
  • Also looking at your regex: `[\s+]` matches one occurence of a whitespace character or the `+` character ; you most likely want `\s+` which matches one or more whitespace (or maybe `\s*` actually); `[-]` and `[,]` -> just use `-` and `,` – Aaron Feb 06 '19 at 19:45
  • I think you might have it right, but I think the [\s+] you have at the beginning means 1 or more spaces, I think you probably want \s* (0 or more spaces). Also, I don't think it needs to be in square brackets. – Blair Feb 06 '19 at 19:47
  • Use `^(?![0-9]{3}-[a-zA-Z]{3}-[0-9]{4}\s*,\s*[0-9]{2,3}).*`, see [this demo](https://regex101.com/r/CFF3vP/1). – Wiktor Stribiżew Feb 06 '19 at 20:42

0 Answers0