[英]Regex to match unescaped pairs of braces
I'm currently trying to make a regex to match the closest pair of an opening and closing bracket without matching escaped braces. 我目前正在尝试制作一个正则表达式以匹配最接近的一对开合括号,而不匹配转义括号。 (And without matching double escape characters infront of a non escaped bracket).
(并且在非转义括号的前面没有匹配的双转义字符)。
{"asd"} - {"asd"}
\{"test"} - No match
\\{"Apple juice}"} - {"Apple juice}"}
{{"Test"}} - {"Test"}
I'd already be glad to find a regex which just maches unescaped characters without using \\K
I have tried multiple things but I can't figure out why this regex doesn't work: ((?<!\\\\)(?=(?:\\\\{2})*)){
我已经很高兴找到一个无需使用
\\K
就可以处理未转义字符的正则表达式,我已经尝试了多种方法,但是我不知道为什么这个正则表达式不起作用: ((?<!\\\\)(?=(?:\\\\{2})*)){
You can use 您可以使用
(?<!\\)\\[{}]|(\{(?:"[^"\\]*(?:\\.[^"\\]*)*"|[^{}]*)(?:\\.(?:"[^"\\]*(?:\\.[^"\\]*)*"|[^{}]*))*})
See regex demo 见正则表达式演示
The regex matches 2 alternatives: 正则表达式匹配2个替代方案:
(?<!\\\\)\\\\[{}]
- an escaped opening or closing brace with no \\
in front (?<!\\\\)\\\\[{}]
-前面没有\\
的转义大括号 OR (this one is captured, so you can grab it by accessing the first capturing group) 或(已捕获该对象,因此您可以通过访问第一个捕获组来捕获它)
(\\{(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*)(?:\\\\.(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*))*})
- matches and captures into Group 1 a substring starting with (\\{(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*)(?:\\\\.(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*))*})
-匹配并捕获一个从1开始的子字符串到组1中
\\{
- opening brace \\{
-开括号 (?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*)
- a quoted string containing any escaped sequences ( "[^"\\\\]*(?:\\\\.[^"\\\\]*)*")
or 0 or more characters other than {
and }
( [^{}]*
) (?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*)
-包含任何转义序列的带引号的字符串( "[^"\\\\]*(?:\\\\.[^"\\\\]*)*")
或0个或多个{
和}
以外的字符( [^{}]*
) (?:\\\\.(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*))*
- matches 0 or more sequences of... (?:\\\\.(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*))*
-匹配0或更大...的顺序
\\\\.
- escaped sequence (?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*)
- see description above (?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^{}]*)
-请参见上面的说明 }
- closing brace }
-右括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.