簡體   English   中英

python 正則表達式:提取單引號或雙引號之間的文本

[英]python regex: extracting texts between single or double quotation marks

我想要做的是提取單引號或雙引號之間的所有值。

假設我有以下值。

"Alice's Adventures in Wonderland 1" 
"Alice's 'Adventures' in Wonderland 1" 
"Alice's "Adventures" in Wonderland 1" 
"Alice's Adventures \nin Wonderland 1" 
'Alice's Adventures in Wonderland 1'
'Alice's "Adventures" in Wonderland 1'
'Alice's 'Adventures' in Wonderland 1'
'Alice's Adventures \tin Wonderland 1'

所需的輸出是:

Alice's Adventures in Wonderland 1
Alice's 'Adventures' in Wonderland 1
Alice's "Adventures" in Wonderland 1
Alice's Adventures \nin Wonderland 1
Alice's Adventures in Wonderland 1
Alice's "Adventures" in Wonderland 1
Alice's 'Adventures' in Wonderland 1
Alice's Adventures \tin Wonderland 1

我應該如何編寫正則表達式(使用一個正則表達式來一次提取所有所需的值)以獲取包含在第一個和最后一個引號中的整個文本?

ps 我想用re.search(r"...", text)方法

(?<?[\'\"])\n)?部分包括\n進入實際文本之間。 \1到末尾以匹配以'"開頭的引號

for match in re.finditer(r'^([\'\"])(.*?(?:(?<![\'\"])\n)?.*?)\1 *$', str1, re.M):
    print(match.group(2))

Alice's Adventures in Wonderland 1
Alice's 'Adventures' in Wonderland 1
Alice's "Adventures" in Wonderland 1
Alice's Adventures 
in Wonderland 1
Alice's Adventures in Wonderland 1
Alice's "Adventures" in Wonderland 1
Alice's 'Adventures' in Wonderland 1
Alice's Adventures  in Wonderland 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM