Eric
Eric

Reputation: 23

Regex replace with negative look ahead in Python

I am trying to delete the single quotes surrounding regular text. For example, given the list:

alist = ["'ABC'", '(-inf-0.5]', '(4800-20800]', "'\\'(4.5-inf)\\''", "'\\'(2.75-3.25]\\''"]

I would like to turn "'ABC'" into "ABC", but keep other quotes, that is:

alist = ["ABC", '(-inf-0.5]', '(4800-20800]', "'\\'(4.5-inf)\\''", "'\\'(2.75-3.25]\\''"]

I tried to use look-head as below:

fixRepeatedQuotes = lambda text: re.sub(r'(?<!\\\'?)\'(?!\\)', r'', text)
print [fixRepeatedQuotes(str) for str in alist]

but received error message:

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

Any other workaround? Thanks a lot in advance!

Upvotes: 2

Views: 2598

Answers (2)

Cylian
Cylian

Reputation: 11182

Try should work:

result = re.sub("""(?s)(?:')([^'"]+)(?:')""", r"\1", subject)

explanation

"""
(?:         # Match the regular expression below
   '           # Match the character “'” literally (but the ? makes it a non-capturing group)
)
(           # Match the regular expression below and capture its match into backreference number 1
   [^'"]       # Match a single character NOT present in the list “'"” from this character class (aka any character matches except a single and double quote)
      +           # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?:         # Match the regular expression below
   '           # Match the character “'” literally (but the ? makes it a non-capturing group)
)
"""

Upvotes: 1

Joel Cornett
Joel Cornett

Reputation: 24788

re.sub accepts a function as the replace text. Therefore,

re.sub(r"'([A-Za-z]+)'", lambda match: match.group(), "'ABC'")

yields

"ABC"

Upvotes: 1

Related Questions