-1

Code:

text = "('hel'lo') eq 'some 'variable he're'"
re.compile(r"(?<!\(|(?<=eq ))'(?!\)|\Z)").sub(string=text, repl="''")

Getting error:

re.error: look-behind requires fixed-width pattern

Expected output:

('hel''lo') eq 'some ''variable he''re'

User1493
  • 481
  • 2
  • 7
  • 23

1 Answers1

1

If you want to assert that what is on the left is not eq it should be a negative lookbehind (?<! instead of a positive lookbehind.

You can write the pattern using 2 lookbehind assertions.

(?<!\()(?<!eq )'(?!\)|\Z)

Regex demo | Python demo

Example code

import re
text = "('hel'lo') eq 'some 'variable he're'"
print(re.compile(r"(?<!\()(?<!eq )'(?!\)|\Z)").sub(string=text, repl="''"))

Output

('hel''lo') eq 'some ''variable he''re'
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    I was trying to use ALTERNATIVES which was causing me the error. Two Look behind also works.. thank you very much – User1493 Mar 19 '20 at 09:02