正则表达式匹配 B 的最后一个实例,其中 B 在它后面,但不在 A 之前
Regex match last instance of B where B is behind it, but not before A
我一直在尝试修改我很久以前构建的正则表达式以支持更复杂的输入,但一直无法让它工作。这是当前表达式的示例输入,它已经可以工作,以及我想要支持但无法开始工作的示例输入。
正则表达式
\{([^{}']*\{[\d]+(?:,[^,}]+)*}.*?)}
工作样本输入
{{0}}
{{0,test}}
{test {0,test}}
{{0,test} test2}
{test {0,test} test2}
输入我要支持
test {test {0, test} test2 {1} test3 {2}}
test {test {0, test} test2 {1} test3 {2}} {3}
test {test {0, test} test2 {1} test3 {2}} asdf
{test {0, test} test2 {1} test3 {2}}
{test {0, test} test2 {1} test3 {2} test4}
{test {0, test} test2 {1} test3 {2} test4} test5 {3}
在我想要支持的示例中,它应该找到 }
的最后一个实例,它后面有 }
,但没有 {
。
有效匹配(我想要这些)
test {test {0, test} test2 {1} test3 {2}} -> test {0, test} test2 {1} test3 {2}
test {test {0, test} test2 {1} test3 {2}} {3} -> test {0, test} test2 {1} test3 {2}
test {test {0, test} test2 {1} test3 {2}} asdf -> test {0, test} test2 {1} test3 {2}
{test {0, test} test2 {1} test3 {2}} -> test {0, test} test2 {1} test3 {2}
{test {0, test} test2 {1} test3 {2} test4} -> test {0, test} test2 {1} test3 {2} test4
{test {0, test} test2 {1} test3 {2} test4} test5 {3} -> test {0, test} test2 {1} test3 {2} test4
{test {0, test} test2 {1} test3 {2} test4} test5 {3 -> test {0, test} test2 {1} test3 {2} test4
无效匹配(我不希望这些出现在固定的正则表达式中)
{test {0, test} test2 {1} test3 {2} test4} test5 {3} -> test {0, test} test2 {1} test3 {2} test4} test5 {3
我已经花了 2 个小时在 https://regex101.com 上尽我所能修改它,但无济于事。
为了澄清标签,正则表达式适用于 Java 正则表达式方言,因此虽然它是 PCRE,但某些 boundaries/operators 不受支持。
我不确定你想捕捉什么,但这是一个有效的正则表达式:
{[^{}]*(({[^{}]+})[^{}]*)+}
测试here.
开头和结尾的花括号是你感兴趣的。
您可以匹配左大括号和右大括号,并在两者之间使用捕获组,您可以在其中匹配从大括号开始到结束大括号及其前后的非大括号内容。
该值在组 1 中。
{((?:[^{}]*{[^{}]*}[^{}]*)*)}
说明
{
匹配开头卷曲
(
捕获 组 1
(?:
非捕获组
[^{}]*
匹配 0+ 次除 {
或 }
之外的任何字符
{[^{}]*}
从开始 {
到结束 }
匹配
[^{}]*
匹配 0+ 次除 {
或 }
之外的任何字符
)*
关闭非捕获组并重复0+次
)
关闭组 1
}
匹配结束卷曲
我一直在尝试修改我很久以前构建的正则表达式以支持更复杂的输入,但一直无法让它工作。这是当前表达式的示例输入,它已经可以工作,以及我想要支持但无法开始工作的示例输入。
正则表达式
\{([^{}']*\{[\d]+(?:,[^,}]+)*}.*?)}
工作样本输入
{{0}}
{{0,test}}
{test {0,test}}
{{0,test} test2}
{test {0,test} test2}
输入我要支持
test {test {0, test} test2 {1} test3 {2}}
test {test {0, test} test2 {1} test3 {2}} {3}
test {test {0, test} test2 {1} test3 {2}} asdf
{test {0, test} test2 {1} test3 {2}}
{test {0, test} test2 {1} test3 {2} test4}
{test {0, test} test2 {1} test3 {2} test4} test5 {3}
在我想要支持的示例中,它应该找到 }
的最后一个实例,它后面有 }
,但没有 {
。
有效匹配(我想要这些)
test {test {0, test} test2 {1} test3 {2}} -> test {0, test} test2 {1} test3 {2}
test {test {0, test} test2 {1} test3 {2}} {3} -> test {0, test} test2 {1} test3 {2}
test {test {0, test} test2 {1} test3 {2}} asdf -> test {0, test} test2 {1} test3 {2}
{test {0, test} test2 {1} test3 {2}} -> test {0, test} test2 {1} test3 {2}
{test {0, test} test2 {1} test3 {2} test4} -> test {0, test} test2 {1} test3 {2} test4
{test {0, test} test2 {1} test3 {2} test4} test5 {3} -> test {0, test} test2 {1} test3 {2} test4
{test {0, test} test2 {1} test3 {2} test4} test5 {3 -> test {0, test} test2 {1} test3 {2} test4
无效匹配(我不希望这些出现在固定的正则表达式中)
{test {0, test} test2 {1} test3 {2} test4} test5 {3} -> test {0, test} test2 {1} test3 {2} test4} test5 {3
我已经花了 2 个小时在 https://regex101.com 上尽我所能修改它,但无济于事。
为了澄清标签,正则表达式适用于 Java 正则表达式方言,因此虽然它是 PCRE,但某些 boundaries/operators 不受支持。
我不确定你想捕捉什么,但这是一个有效的正则表达式:
{[^{}]*(({[^{}]+})[^{}]*)+}
测试here.
开头和结尾的花括号是你感兴趣的。
您可以匹配左大括号和右大括号,并在两者之间使用捕获组,您可以在其中匹配从大括号开始到结束大括号及其前后的非大括号内容。
该值在组 1 中。
{((?:[^{}]*{[^{}]*}[^{}]*)*)}
说明
{
匹配开头卷曲(
捕获 组 1(?:
非捕获组[^{}]*
匹配 0+ 次除{
或}
之外的任何字符
{[^{}]*}
从开始{
到结束}
匹配
[^{}]*
匹配 0+ 次除{
或}
之外的任何字符
)*
关闭非捕获组并重复0+次
)
关闭组 1}
匹配结束卷曲