2012-08-09 103 views
1

我試圖刪除圍繞普通文本的單引號。例如,給出的列表:正則表達式用Python中的負面展望替換

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

我想談談 「 'ABC'」 到 「ABC」,但保留其他的報價,那就是:

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

我試圖用look-如下文頭:

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

但收到錯誤消息:

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

任何其他解決方法?提前感謝!

+0

你想幹什麼' 「 '\\'(4.5-INF)\\ ''」'產生? – 2012-08-09 04:13:17

+0

我想保持不變。 – Eric 2012-08-09 04:14:15

+0

好的,那麼你對「普通文本」的定義是什麼?字母數字A-Z? – 2012-08-09 04:17:53

回答

1

嘗試應該工作:

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

解釋

""" 
(?:   # 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 
) 
""" 
+0

感謝您的回答和解釋! – Eric 2012-08-09 05:33:53

1

re.sub接受一個函數作爲替換文本。因此,

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

產生

"ABC" 
+0

謝謝喬爾,它解決了我的問題。它應該是match.group(1)。 – Eric 2012-08-09 05:24:53

+0

@Eric:我認爲你將'.group()'與'groups()'混淆。 (參考上述評論。) – 2012-08-09 05:39:22