首页 > 解决方案 > 返回对话行的正则表达式

问题描述

-我是初学者 python 编码器,所以请多多包涵!

到目前为止我所拥有的是,

def q_4():
  pattern = r'^\"\w*\"'
  return re.compile(pattern, re.M|re.IGNORECASE)

但由于某种原因,它只返回一个实例,两个双引号之间有一个单词。我怎样才能掌握完整的线条?

标签: pythonpython-3.x

解决方案


尝试搜索模式\"[^"]+\"

inp = """Here is a quote: "the quick brown fox jumps over
the lazy dog" and here is another "blah
blah blah" the end"""

dialogs = re.findall(r'\"([^"]+)\"', inp)
print(dialogs)

这打印:

['the quick brown fox jumps over\nthe lazy dog', 'blah\nblah blah']

推荐阅读