Java RegEx:只需获取匹配器组的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4142856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:59:29  来源:igfitidea点击:

Java RegEx: Just get a part of the matcher group

javaregex

提问by Tobias

I have a regex in Java:

我在 Java 中有一个正则表达式:

Pattern pattern = Pattern.compile(<string>text</string><string>.+</string>);
Matcher matcher = pattern.matcher(ganzeDatei);
while (matcher.find()) {
    String string = matcher.group();
    ...

This works fine, but the output is something like

这工作正常,但输出类似于

<string>text</string><string>Name</string>

<string>text</string><string>Name</string>

But I just want this: Name

但我只想要这个: Name

How can I do this?

我怎样才能做到这一点?

回答by Andrzej Doyle

Capture the text you want to return by wrapping it in parenthesis, so in this example your regex should become

通过将其括在括号中来捕获要返回的文本,因此在此示例中,您的正则表达式应变为

<string>text</string><string>(.+)</string>

Then you can access the text that matched between the parenthesis with

然后您可以访问括号之间匹配的文本

matcher.group(1)

The no-arg groupmethod you are calling, returns the entire portion of the input text that matches your pattern, whereas you want just a subsequence of that, which matches a capturing group (the parenthesis).

您正在调用的 no-arg group方法返回与您的模式匹配的输入文本的整个部分,而您只需要一个与捕获组(括号)匹配的子序列。

回答by Sean Patrick Floyd

Then do this:

然后这样做:

Pattern pattern = Pattern.compile(<string>text</string><string>(.+)</string>);
        Matcher matcher = pattern.matcher(ganzeDatei);
        while (matcher.find()) {
            String string = matcher.group(1);
            ...

Reference:

参考:

回答by Micha? Niklas

You must put text you want to obtain by group()into brackets. So use:

您必须将要获取的文本group()放入括号中。所以使用:

<string>(.+)</string>