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:

JavaScript

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:

JavaScript

Advertisement

Answer

JavaScript

Output:

JavaScript

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