簡體   English   中英

正則表達式查找引號內的句子

[英]Regex to find sentences inside quotation marks

我有一個很長的文本,其中一些行沒有引號,其他一些行用雙引號括起來,而其他一些行只部分用引號括起來。

這是一段摘錄(每一行都是上述情況的一個例子):

#Example 1 (line with no quotation marks)
I thought all this over for two or three days, and then I reckoned I would see if there was anything in it. 

#Example 2 (full line inside quotation marks)
"Why, my boy, you are all out of breath.  Did you come for your interest?" 

#Example 3 (only part of the line inside quotation marks)
"No, sir," I says, "I don't want to spend it. 

我正在嘗試找到一個正則表達式來找到所有這些行:

  • 從換行開始
  • 開頭有雙引號
  • 末尾有雙引號

換句話說,遵循上面第二個示例的行。 我嘗試了以下方法:

import re

def my_pattern():
  pattern = r'^\"(.+)\"$'
  return re.compile(pattern, re.M | re.IGNORECASE)

但我沒有得到我想要的 output。 關於如何改進我的正則表達式的任何想法?

而不是匹配中間的所有內容。 嘗試僅匹配非"和非\n字符。

^"([^"\n]+)"$

暫無
暫無

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

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