我想捕获关键字“ end”之前括号内的所有ASCII字符串。但是,我只想捕获第一个匹配组。
如何忽略第二个匹配组?
这是我编写的示例正则表达式:\((.+?)\) end
这是我使用的示例字符串:"There are some other sentences before (some otherwords which I am not interested in) all these.This is a sample string (something which I am interested in) end. This is another repeated string (with some otherwords) end."
我只想获取括号中的输出“我感兴趣的东西”。
答案 0 :(得分:1)
首先让我回答您的原始问题。
我想捕获括号前的所有ASCII字符串 关键字“结束”。但是,我只想捕捉第一个 匹配组。
如何忽略第二个匹配组?
输入:
There are some other sentences before (some otherwords which I am not interested in) all these.This is a sample string (something which I am interested in) end. This is another repeated string (with some otherwords) end.
预期的捕获量:
somethings which I am interested in
要使用的正则表达式:
^(?<!\) end).*?\(([^()]+?)\) end
演示: https://regex101.com/r/dVo9Zi/1
其他说明:
如果括号中有括号,我们将遇到问题 与正则表达式。我不确定是否可以提取此类关键字。
如果您需要分析嵌套结构,则必须忽略正则表达式和解析器,如此处所述:Can regular expressions be used to match nested patterns?
[^()]
并将其替换为所有ASCII字符的十六进制连续间隔,您将将必须明确排除(
和)
。这为您提供了以下字符类:[\x00-\x27\x2A-\x7F]
。 参考:http://www.asciitable.com/,演示: https://regex101.com/r/dVo9Zi/2