Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两种文本消息 - 都在开头有一个统一的消息代码,但可能有第二个匹配包含在 ' 字符中,如果存在我需要提取它。
M0123具有两个预期匹配项的示例“ extratext ”。
M0321没有两个匹配的示例
这匹配 #1 并捕获两个组但不匹配 #2:
^(?<code>M\d+).*(?<extra>'.*').*
这与 #1 & #2 匹配,但从未捕获过额外的组:
^(?<code>M\d+).*(?<extra>'.*')?.*
否定字符类应该可以帮助你,比如
^(?<code>M\d+)[^']*(?:(?<extra>'.*').*)?
将第一个.*转换为[^']*将使其与您的第一个样本的第一个引号和第二个样本的整个字符串匹配。
.*
[^']*
笔记:
(?<extra>'[^']*')