Skip to content
Advertisement

Match everything except certain strings

I am trying to write python script to match a regex that can include everything which has two - and one . but I also want to exclude two strings from it. They are NIST-Privacy-v1.1 and NIST-CSF-v1.1

Here is my sample data:

NIST-Privacy-v1.1
NIST-CSF-v1.1
AWS-CIS-v1.4-1.10
SOC-2-CC6.8
NIST-800-53rev5
NIST-800-53rev5-CM-3(1)
NIST-800-53rev5-AU-12(4)
SOC-2-CC6.1
NISTPrivacyFramework-v1.0
NISTPrivacyFramework-v1.0-PR.AC-P1
NIST-800-53rev5-AC-1
NIST-800-53rev5-IA-1

I started with a very simple regex which does the job of matching what I need but doesn’t exclude the two strings. Can you help me identify the exclusion part.

regex: .*-.*-.*[.|-].*

Desired output:

AWS-CIS-v1.4-1.10
SOC-2-CC6.8
NIST-800-53rev5-CM-3(1)
NIST-800-53rev5-AU-12(4)
SOC-2-CC6.1
NISTPrivacyFramework-v1.0-PR.AC-P1
NIST-800-53rev5-AC-1
NIST-800-53rev5-IA-1

Advertisement

Answer

^(?!NIST-Privacy-v1.1)(?!NIST-CSF-v1.1).*-.*-.*[.-].*$

Output:

AWS-CIS-v1.4-1.10
SOC-2-CC6.8
NIST-800-53rev5-CM-3(1)
NIST-800-53rev5-AU-12(4)
SOC-2-CC6.1
NISTPrivacyFramework-v1.0-PR.AC-P1
NIST-800-53rev5-AC-1
NIST-800-53rev5-IA-1

Demo: https://regex101.com/r/gM9e44/1

  • ^ => Given pattern must start from the beginning of the line
  • [.-] => “-” or “.”
  • ^(?!NIST-Privacy-v1.1) => It must not start with “NIST-Privacy-v1.1”
  • ^(?!NIST-Privacy-v1.1)(?!NIST-CSF-v1.1) => It must not start with “NIST-Privacy-v1.1” or “NIST-CSF-v1.1”
  • $ => Given pattern must finish at the end of the line
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement