正则表达式匹配两个{}之间的所有内容 [英] Regex match everything between two {}
问题描述
我在这里正在寻找不同的答案,但是不幸的是,这些答案都不适合我的情况.所以我希望你不要介意.
I was looking at different answers here but unfortunately none of them was good for my case. So I hope you don't mind about it.
因此,我需要匹配两个大括号{}之间的所有内容,除非匹配以@开头且没有这些大括号的情况,例如:
So I need to match everything between two curly brackets {} except situation when match starts with @ and without these curly brackets e.g:
- 这是超级文本{ match_this }"
- "{ match_this }"
- 这是@ {deal_with_it}的另一个示例"
- "This is a super text {match_this}"
- "{match_this}"
- "This is another example @{deal_with_it}"
这是我的测试字符串,1,2,3有效,而最后一个不应该是:
Here are my test strings, 1,2,3 are valid while the last one shouldn't be:
1 {eww}
2 r23r23{fetwe}
3 #{d2dded}
4 @{d2dded}
我正在尝试:
(?<=[^@]\{)[^\}]*(?=\})
然后只有第2个和第3个选项是匹配项(没有第一个选项) https://regex101.com/r/qRInUf/2/
Then only 2th and 3th options were matches (without the first one) https://regex101.com/r/qRInUf/2/
然后我正在尝试:
\{(.*?)\} or [^@]\{(.*?)\}
在两种情况下我都无法匹配1,2,3值 https://regex101.com/r/uYRtLD/1
In both cases I was unable to match 1,2,3 values https://regex101.com/r/uYRtLD/1
提前感谢您的任何建议.
Thanks in advance for any suggestions.
这是针对Java的.
推荐答案
(?<=(?<!@)\{)[^}]*(?=})
-
(?<=(?<!@)\{)
确保后面的正向查找符合以下条件-
(?<!@)
负向查找,确保后面的内容与@
字面不符 -
\{
从字面上匹配{
. (?<=(?<!@)\{)
Positive lookbehind ensuring what precedes matches the following(?<!@)
Negative lookbehind ensuring what precedes doesn't match@
literally\{
Match{
literally.
结果:
{eww} # Matches eww r23r23{fetwe} # Matches fetwe #{d2dded} # Matches d2dded @{d2dded} # Does not match this because @ precedes {
这篇关于正则表达式匹配两个{}之间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-