1

I have the following RegEx patterns:

  1. "[0-9]{4,5}\.FU|[0-9]{4,5}\.NG|[0-9]{4,5}\.SP|[0-9]{4,5}\.T|JGB[A-Z][0-9]|JNI[A-Z][0-9]|JN4F[A-Z][0-9]|JNM[A-Z][0-9]|JTI[A-Z][0-9]|JTM[A-Z][0-9]|NIY[A-Z][0-9]|SSI[A-Z][0-9]|JNI[A-Z][0-9]-[A-Z][0-9]|JTI[A-Z][0-9]-[A-Z][0-9]" ===> matches 8411.T or JNID8
  2. "[0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]" ==> matches 9345.HK or HCEIU9-A9
  3. ".*\.SI|SFC[A-Z][0-9]" ==> matches 8345.SI or SFCX8

How can I obtain a RegEx from the negation of these patterns? I want to match strings that match neither of these 3 patterns: e.g. I want to match 8411.ABC, but not any of the aforementioned strings (8411.T, HCEIU-A9, 8345.SI, etc.).

I've tried (just to exclude 2 and 3 for instance, but it doesn't work):

^(?!((.*\.SI|SFC[A-Z][0-9])|([0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]))) 
Thom A
  • 88,727
  • 11
  • 45
  • 75
Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59

1 Answers1

1

The main idea here is to place the patterns into (?!.*<pattern>) negative lookaheads anchored at the start of the string (^). The difficulty here is that you patterns contain unanchored alternations, and if not grouped, the .* before the patterns will only refer to the first alternative (i.e. all the subsequent alternatives will only be negated at the start of the string.

Thus, your pattern formula is ^(?!.*(?:<PATTERN1>))(?!.*(?:<PATTERN2>))(?!.*(?:<PATTERN3>)). Note that .+ or .* at the end is optional if you need to just get a boolean result. Note that in the last pattern, you need to remove the .* in the first alternative, it won't make sense to use .*.*.

Use

^(?!.*(?:[0-9]{4,5}\.FU|[0-9]{4,5}\.NG|[0-9]{4,5}\.SP|[0-9]{4,5}\.T|JGB[A-Z][0-9]|JNI[A-Z][0-9]|JN4F[A-Z][0-9]|JNM[A-Z][0-9]|JTI[A-Z][0-9]|JTM[A-Z][0-9]|NIY[A-Z][0-9]|SSI[A-Z][0-9]|JNI[A-Z][0-9]-[A-Z][0-9]|JTI[A-Z][0-9]-[A-Z][0-9]))(?!.*(?:[0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]))(?!.*(?:\.SI|SFC[A-Z][0-9])).+

See the regex demo.

You may also contract the formula to ^(?!.*(?:<PATTERN1>|<PATTERN2>|<PATTERN3>)):

^(?!.*(?:[0-9]{4,5}\.FU|[0-9]{4,5}\.NG|[0-9]{4,5}\.SP|[0-9]{4,5}\.T|JGB[A-Z][0-9]|JNI[A-Z][0-9]|JN4F[A-Z][0-9]|JNM[A-Z][0-9]|JTI[A-Z][0-9]|JTM[A-Z][0-9]|NIY[A-Z][0-9]|SSI[A-Z][0-9]|JNI[A-Z][0-9]-[A-Z][0-9]|JTI[A-Z][0-9]-[A-Z][0-9]|[0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]|\.SI|SFC[A-Z][0-9])).+

See another regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • What is the purpose of "?:"? what does it do? – Tamas Ionut Sep 06 '17 at 09:36
  • 1
    See [What is a non-capturing group? What does a question mark followed by a colon (?:) mean?](https://stackoverflow.com/questions/3512471). Just groups alternatives without storing the value in the memory. – Wiktor Stribiżew Sep 06 '17 at 09:38