当前位置: 首页 > 知识库问答 >
问题:

python正则表达式:在单引号或双引号之间提取文本

沈开畅
2023-03-14

我想做的是提取单引号或双引号之间的所有值。

假设我有以下值。

"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

我应该如何编写正则表达式(使用一个正则表达式一次提取所有所需的值),以获得包含在第一个和最后一个引号中的整个文本?

p、 我想使用re。搜索(r“…”,文本)方法

共有1个答案

扈翰
2023-03-14

<代码>(?

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
 类似资料:
  • 问题内容: 我写了一个正则表达式,用double-qoutes分割字符串: 如何将其扩展为与单双qoutes一起使用? 我试过了: 但这是行不通的 问题答案: 有两种方法:

  • 有没有一种方法使组的“capture”可以在regex后面引用,但其捕获的值不在匹配列表中返回? 或者其他一些方法来解决我(看似简单)的问题。

  • 我有一个应用程序收到一个格式错误的JSON字符串,如下所示: null 也可能是格式错误的JSON字符串: 在本例中,不应替换消息值中的单引号。

  • 我正在编写一个正则表达式来解析包含标记字段的行。标签出现在等号之前,内容出现在等号之后,用单引号或双引号括起来。对于大多数字段,内容都用单引号括起来。如果字段的内容包含单引号,则该字段用双引号括起来。例如: 我的正则表达式有效,除非字段用双引号括起来。 在Debuggex中测试 在Regexr中测试 对于上面示例中标记为a的字段,a=“'D08/APPL'”,a由捕获组1匹配,后面的单引号由捕获组

  • 我正在尝试编写一个正则表达式模式,并使用split函数对所有句点进行拆分,除非这些句点位于开括号和闭括号或花括号之间。我想我是对的,因为在一个在线正则表达式测试程序中,当我运行拆分时,它正确地突出显示了我想要拆分的周期,它拆分的不仅仅是周期,还创建了一个长列表。我想我正在做一些明显的事情,但似乎无法理解。请看下面的内容。谢谢 到目前为止我所拥有的: Regex到目前为止: .(? =([^]]([