Search code examples
pythonregexregex-lookaroundsnegative-lookbehind

re.sub() negative look behind + negative look ahead


Remove every occurence of ' from a string except when a word ends with s, if word ends with s' or 's it the ' is left in. EVERY other occurrence is removed.

Example:

Andrea's -Stays as is
Kids' - stays as is
'Kids' --> Kids
Ki'd's' --> Kids'

WHat I came up with so far :

\'(?!s ) 

this matches the first example and ignores it.

here is it working

I have a problem with the rest


Solution

  • '([^'\s]+)'
    

    You can try this.Replace by \1.See demo.

    https://regex101.com/r/oF9hR9/2#python