I'm looking for Regular expressions to exclude a specific substring from a match

158 Views Asked by At

Basically I have these strings and I'm programming on Python 3.9 :

'P425-TK-1501'
'P425-UN-1840'
'P900-TP-1001'

What if I want to match each of these strings EXCEPT the one with TP (P900-TP-1001).

As you can see, my challenge here was to INCLUDE the P425-TK-1501 but EXCLUDE P900-TP-1001

I tried excluding but it doesn't seem to work with what I need.

[UPDATE] So basically, I've written a code to extract specific strings within a txt file. In the text file, there are words, numbers, spaces, etc. The strings I want to extract have specific format which is searched using this Regex : '[BPC][3-9]\d{2}-[A-Z]{2}-[1-2]\d{3}' .

What it does :

  1. Searches for B, P or C for the first character.
  2. Searches for number from 3 to 9 for the second.
  3. Searches for any number for the third & fourth.
  4. Searches for a hyphen for the fifth.
  5. Searches for a letter from A to Z for the sixth and seventh.
  6. Etc...

What I would like, is for the Regex to EXCLUDE the specific strings 'TP' (in that order), WITHOUT excluding 'TK' which may occur multiple times in the text file.

1

There are 1 best solutions below

0
On

To exclude a substring, simply accept the strings which do not have it. No regex needed.

data = ['P425-TK-1501', 'P425-UN-1840', 'P900-TP-1001']

new = [x for x in data if 'P900' not in x]

produces

['P425-TK-1501', 'P425-UN-1840']