如果它們是一兩個(gè)詞長(zhǎng),我想提取引號(hào)中的詞。這適用于以下代碼。mysentences = ['Kids, you "tried" your "best" and you failed miserably. The "lesson" is, "never try."', "Just because I don’t 'care' doesn’t mean I don’t understand."]quotation = []rx = r'"((?:\w+[ .]*){1,2})"' for sentence in mysentences: quotation.append(re.findall(rx, sentence))print(quotation)但這并沒有讓我從第二句中得到“關(guān)心”,因?yàn)榈诙溆秒p引號(hào)引起來。我可以通過以下方式獲得它r"'((?:\w+[ .]*){1,2})'"問題是,我怎樣才能加入條件?和rx = r'"((?:\w+[ .]*){1,2})"' or r"'((?:\w+[ .]*){1,2})'"它只會(huì)讓我得到第一個(gè)提到的條件。
如何用正則表達(dá)式同時(shí)搜索兩個(gè)可能的引號(hào)?
千萬(wàn)里不及你
2022-04-23 16:51:59