Home > Net >  Inverse Match in Regex
Inverse Match in Regex

Time:12-21

I have a string

<img width="24" src="https://someurl.com" height="24" alt="FirstName LastName" id="ember44" > id="ember44" >

In the RegEx I need to select everything except

alt="FirstName LastName"

Tried some kind of expression

alt. (?!alt)

but still not in place. Thank you in advance!

CodePudding user response:

Instead of trying to match everything that isn't your "anti-search" string, how about replacing that string with nothing?

s = """<img width="24" src="https://someurl.com" height="24" alt="FirstName LastName" id="ember44" > id="ember44" >"""

s_new = re.sub(r'alt=\"[^\"] \"\s ', '', s)
# '<img width="24" src="https://someurl.com" height="24" id="ember44" > id="ember44" >'

Explanation (Try online):

alt=\"[^\"] \"\s 
-----------------

alt=\"       \"     : Literally alt=, followed by quotes
      [^\"]         : One or more non-quote characters
               \s   : One or more whitespace

CodePudding user response:

Well, in order to invert a regex, you could use re.sub() which takes 3 required arguments. a pattern, replacement, and a original string.

So, you could invert like this

import re

s = '<img width="24" src="https://someurl.com" height="24" alt="FirstName LastName" id="ember44" > id="ember44" >'

pattern = r'alt=".*?"'
without_alt = re.sub(pattern, '', s)
print(without_alt)
  • Related