I have a file that contains this:
我有一个包含这个的文件:
dependencies {
compile group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
compile group: "org.osgi", name: "org.osgi.core", version: "6.0.0"
}
deploy {
deployDir = file("${Home}/osgi/modules")
}
and I want to extract the information inside dependencies bracket.
我想提取依赖项括号内的信息。
When I tried to use dependencies.*[^}]
I would get everything except the close bracket for deploy.
当我尝试使用依赖项。* [^}]我会得到除了用于部署的关闭括号之外的所有内容。
How can I get the regex to stop at the first encounter of close bracket?
如何在第一次遇到紧密支架时让正则表达式停止?
3 个解决方案
#1
2
Use a non-greedy search
使用非贪婪的搜索
dependencies.*?[^}]}
#2
0
You can use a negated character class [^}]
with a *
quantifier to get to the closing brace, and just use }
to match it as a literal symbol (it does not need escaping):
您可以使用带有*量词的否定字符类[^}]来获得结束括号,并使用}将其匹配为文字符号(它不需要转义):
dependencies[^}]*}
See regex demo
请参阅正则表达式演示
No need for any look-arounds here that only hamper regex performance.
这里不需要任何只能阻碍正则表达式性能的环境。
#3
0
Simple to understand way is to match non-close braces:
简单易懂的方法是匹配非紧密的大括号:
(?<=dependencies \{)[^}]*
This has the advantage of not requiring the dot all flag (that makes dot match newlines).
这样做的好处是不需要dot all标志(这使得点匹配换行符)。
You also don't have to worry, or understand, or use, greedy/reluctant quantifiers.
您也不必担心,理解或使用贪婪/不情愿的量词。
The use of the look behind means that the entire match (not a captured group) is your target.
使用后面的外观意味着整个匹配(不是捕获的组)是您的目标。
#1
2
Use a non-greedy search
使用非贪婪的搜索
dependencies.*?[^}]}
#2
0
You can use a negated character class [^}]
with a *
quantifier to get to the closing brace, and just use }
to match it as a literal symbol (it does not need escaping):
您可以使用带有*量词的否定字符类[^}]来获得结束括号,并使用}将其匹配为文字符号(它不需要转义):
dependencies[^}]*}
See regex demo
请参阅正则表达式演示
No need for any look-arounds here that only hamper regex performance.
这里不需要任何只能阻碍正则表达式性能的环境。
#3
0
Simple to understand way is to match non-close braces:
简单易懂的方法是匹配非紧密的大括号:
(?<=dependencies \{)[^}]*
This has the advantage of not requiring the dot all flag (that makes dot match newlines).
这样做的好处是不需要dot all标志(这使得点匹配换行符)。
You also don't have to worry, or understand, or use, greedy/reluctant quantifiers.
您也不必担心,理解或使用贪婪/不情愿的量词。
The use of the look behind means that the entire match (not a captured group) is your target.
使用后面的外观意味着整个匹配(不是捕获的组)是您的目标。