我在 python 脚本中使用正则表达式来捕获命名组。该组出现在分隔符字符串“ S ”之前或之后。我的困惑来自无法在同一个正则表达式中两次使用命名捕获组。
我想使用以下无效(命名组使用两次)正则表达式:
(?:^STD_S_)(?P<important>.+?)$|(?:^(?P<important>.+?)(?:STD_S_)$
描述:
?: non-capture group ^STD_S_ Starting with some "STD_S_" string which is a standard string plus a delimiter
?P Named important string I want
| OR
^?P stat with important STS_S_$ end with standard
我真的很想命名我捕获的重要组。我可以删除名称并使其正常工作。我还可以将单个表达式拆分为两个表达式(OR 的每一侧一个),然后在 python 脚本中搜索选择要使用哪个表达式进行登录。
谢谢!
示例输入
STD_S_important
important_S_STD
示例输出
important #returned by calling the important named group
important
基于与第二种情况不匹配的注释的正则表达式。
(?:(?:^STD_S_)(?P<important>.+?)$)|(?:^(?P=important)(?:_S_STD)$)