Home > database >  How can I find everything but specific word with a regex?
How can I find everything but specific word with a regex?

Time:03-11

I need a regex expression that excludes a specific word from the match.

Here is an example:

"UNION Alexi Stukov"

I need to Exclude "UNION" from the match.

So I want this to be the result:

 Alexi Stukov

Any help would be appreciated, UNION will not always be in the sentence but it should always be in front.

So here are some examples of rows and their regex match I'm hoping for:

  1. UNION Alexi Stukov: expected match: " Alexi Stuckov"
  2. Tom Brady: expected match: "Tom Brady"
  3. UNION Paul Bunyon: expected match: " Pual Bunyon"

CodePudding user response:

You asked to match everything other than UNION, rather than remove UNION. This regex matches everything except UNION:

\b(?!UNION| ).*

See live demo.

This works for the examples given in the question, however won’t work for the general case of UNION appearing other than first.

CodePudding user response:

As you are using Python, you should use startswith() instead:

data = "UNION Alexi Stukov"

if data.startswith("UNION"):
    data = data[5:]  
print(data)

startswith() checks if the String starts with UNION. If it's the case, the first 5 characters of the String are deleted.


In Python 3.9, you can also use removeprefix():

data = "UNION Alexi Stukov"

data = data.removeprefix("UNION")
print(data)  # Alexi  Stukov

In order to remove UNION whatever its position in the text, it's possible to use replace():

data = "Alexi UNION Stukov"

data = data.replace("UNION", '')
print(data)  # Alexi  Stukov
  • Related