【问题标题】:Using regex to match sentences that may include quote marks ""使用正则表达式匹配可能包含引号“”的句子
【发布时间】:2015-03-15 10:15:08
【问题描述】:

几天来,我一直在尝试编写一个正则表达式,该表达式将捕获以特定字符串开头并以不允许的字符 ([]()-,.!?\/),最重要的是'",但总是会以相同的东西结束和开始(

    "starting string foo (?:[a-zA-z0-9_]|[-,.!?()\[\]\'\"\/]|[\s])+"

这很好用,获取所有以“起始字符串 foo”开头并以

例如-的字符串

     starting string foo Hubble revisits the famous "pillars of creation" with a new lens <

它只捕获

    starting string foo Hubble revisits the famous

但是像

这样的字符串
     starting string foo Buzz Aldrin's self-portrait during Gemini 12 with the Earth reflecting off his visor, 12 November 1966 [2651x2632] <

各种标点符号 (' - [ ,) 都能捕捉到我想要的一切-

    starting string foo Buzz Aldrin's self-portrait during Gemini 12 with the Earth reflecting off his visor, 12 November 1966 [2651x2632]

【问题讨论】:

  • 给一个示例字符串,告诉我们它匹配什么,以及它应该或不应该匹配什么。
  • 完成,编辑成帖子

标签: python regex python-3.x findall


【解决方案1】:

怎么了

/starting string foo (.*)\</

【讨论】:

  • 这行得通,但似乎并没有停止(只是得到了整个剩余的巨大字符串!)。
  • 好的,终于让它工作了,谢谢大家。最后的代码是“起始字符串 foo [^
  • 我认为您实际上可能正在寻找非贪婪匹配。看到这里告诉我,我会修改我的答案stackoverflow.com/questions/11898998/regex-match-non-greedy
猜你喜欢
  • 2022-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
相关资源
最近更新 更多