Pergunta

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!

Foi útil?

Solução

Try should work:

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

explanation

"""
(?:         # Match the regular expression below
   '           # Match the character “'” literally
)
(           # Match the regular expression below and capture its match into backreference number 1
   [^'"]       # Match a single character NOT present in the list “'"”
      +           # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?:         # Match the regular expression below
   '           # Match the character “'” literally
)
"""

Outras dicas

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

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

yields

"ABC"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top