python - 如何使用正则表达式在一两个匹配组之后捕获句子的其余部分?
问题描述
所以我有两个正在使用的句子,我有兴趣根据单词中的字符制作特定的捕获组。所以我有这两个西班牙语句子:
- Yo quiero irme de viaje。
- Yo puedo caminar en la nieve。
第一个捕获组必须是其中一个动词,即。“quiero”和“puedo”所以我用这个正则表达式来做([PpDdQq].*o)
。
第二个捕获组必须是紧跟在动词之后的单词,以“me”结尾,我用(\w*me)
.
现在对于最后一个捕获组,在没有以“-me”结尾的直接单词的情况下,必须是紧跟在第一个捕获组之后的所有单词和空格,或者是紧跟在第二个捕获组之后的所有单词和空格。我使用了以“-me”结尾的直接词,(\w.+)
但它不起作用。
谁能帮我弄清楚为什么?谢谢。以下是完整的正则表达式和正则表达式网站的链接,其中包含要匹配的表达式和示例:
([PpDdQq].*o) |(\w*me)|(\w.+)
解决方案
利用
\b([PpDdQq]\w*o)(?:\s+(\w*me))?\b(.*)
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[PpDdQq] any character of: 'P', 'p', 'D', 'd',
'Q', 'q'
--------------------------------------------------------------------------------
\w* word characters (a-z, A-Z, 0-9, _) (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
o 'o'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\w* word characters (a-z, A-Z, 0-9, _) (0
or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
me 'me'
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \3
推荐阅读
- android - 如何设置最后修改的文件?
- scala - EMR Notebook Scala 内核导入图框库
- php - 如何配置谷歌图表以从行更改为列
- java - entityManager.flush() 会清除二级缓存吗?
- reactjs - 将常量参数绑定到构造函数中的方法是否是一种好习惯
- node.js - 基于持续时间的空闲时间 - NODEJS
- javascript - 如何从对象创建 JSON 文件
- html - 如何将 .ttf 和 .eot 文件嵌入为一种字体?
- xslt-3.0 - 无法使用不同的循环求和 xslt 求和
- sprite-kit - 为 SKAudioNode 创建一个 SKAction 扩展