我已经尝试编写这个正则表达式两天了,但失败了,我现在很绝望,希望得到社区的任何帮助。
以下是一些测试用例,说明了我要实现的目标:
姓名 | 输入 | 第一场 | 第 2 场比赛 |
---|---|---|---|
无匹配 | lorem ipsus |
—— | —— |
没有管道的火柴 | lorem {{ . }} ipsus |
{{ . }} |
—— |
与单管匹配 | lorem {{ . | firstPipe }} ipsus |
{{ . | firstPipe }} |
firstPipe |
双管匹配 | lorem {{ . | firstPipe | secondPipe }} ipsus |
{{ . | firstPipe | secondPipe }} |
firstPipe | secondPipe |
匹配空白容忍 | lorem {{ .| firstPipe |secondPipe }} ipsus |
{{ .| firstPipe |secondPipe }} |
firstPipe |secondPipe |
匹配空白容忍 | lorem {{ .|firstPipe| secondPipe}} ipsus |
{{ . | firstPipe | secondPipe }} |
firstPipe| secondPipe |
我正在寻找的东西应该对空格不敏感。这意味着即使在管道字符、括号或点之前/之后缺少空格,它仍然应该匹配。
匹配 1:将括号内的所有内容与可选的空格匹配并带有点。类似的东西{{\s*.\s*}}
,
匹配 2:这是可选的。这是我无法工作的原因。我会接受在括号结束之前返回管道分隔字符串或每个命名管道的匹配项的行为。
感谢您阅读问题。我希望我能说清楚。
用
{{[^{}|]*(?:\|\s*((?:(?!{{).)*?)\s*)?}}
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
{{ '{{'
--------------------------------------------------------------------------------
[^{}|]* any character except: '{', '}', '|' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the least amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
{{ '{{'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)*? end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
}} '}}'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句