What the regex should return must be: (2001) name?

Is there a NOT operator in Regexes? Like in that string : “(2001) (asdf) (dasd1123_asd 21.01.2011 zqge)(dzqge) name (20019)”

I want to delete all ([0-9a-zA-z _.-:]*) but not the one where it is a year (2001).

So what the regex should return must be the (2001) name.

Hey Mehta,

I understand regex can be time-consuming and frustrating, but once you get the concept, it works like magic. To achieve this, you can use a negative lookahead to exclude the year pattern (yyyy) from being matched. Here’s the corrected regex pattern:

\((?!(?:19|20)\d{2}\))[0-9a-zA-Z _.\-:]*

Explanation:

  1. (: Matches a literal opening parenthesis (.
  2. (?!(?:19|20)\d{2})): Negative lookahead assertion to exclude the pattern (yyyy) where yyyy is a year from 1900 to 2099.
  3. (?:19|20): Non-capturing group matching either 19 or 20.
  4. \d{2}: Matches exactly 2 digits (for the year).
  5. ): Matches a literal closing parenthesis ).
  6. [0-9a-zA-Z _.-:]: Matches any combination of digits, letters, spaces, underscores, dots, dashes, or colons zero or more times.

Hope this helps

This regex pattern will correctly match (2001) but exclude (20019) or any other year that falls outside the range of 1900-2099.

Hey Jeo,

Your answers saved time but @mehta_tvara No, there isn’t a direct “not” operator in regex, but you can achieve a similar effect using a zero-width negative lookahead.

For example, to exclude the year (2001) and match other patterns enclosed in parentheses, you can use the following regex pattern:

((?!2001)[0-9a-zA-Z _.-:]*)

Explanation:

  • \(: Matches a literal opening parenthesis (.
  • (?!2001): Negative lookahead assertion to exclude the pattern 2001 immediately following the opening parenthesis.
  • [0-9a-zA-Z _.\-:]*: Matches any combination of digits, letters, spaces, underscores, dots, dashes, or colons zero or more times.
  • \) : Matches a literal closing parenthesis ).

This pattern will match parentheses pairs that do not contain the year 2001, allowing you to delete or process them as needed.

Hope this helped.

I think If you’re still matching (20019) with the pattern ((?![\d]{4})[0-9a-zA-Z _.-:]+), it might be due to the way the negative lookahead is structured. Here’s a revised version of the pattern:

((?!(?:19|20)\d{2}))[0-9a-zA-Z _.-:]+)

This pattern uses a negative lookahead to exclude the pattern (yyyy) where yyyy is a year from 1900 to 2099. The (?:19|20) non-capturing group matches either 19 or 20, followed by \d{2} to match exactly 2 digits for the year. The rest of the pattern [0-9a-zA-Z _.-:]+ matches any combination of digits, letters, spaces, underscores, dots, dashes, or colons. This pattern should correctly exclude (20019) and only match (2001) as desired.