java.lang.IndexOutOfBoundsException: 无组 1 | 模式匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21484162/
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-08-13 08:57:29  来源:igfitidea点击:

java.lang.IndexOutOfBoundsException: No group 1 | Pattern matching

javaregexscala

提问by Vaibhav Raj

I am trying to extract the value of version from Acceptheader which can be of the form

我试图从Accept标题中提取版本的值,它可以是以下形式

"vnd.example-com.foo+json; version=1.1" 

here is my code for extracting the version

这是我提取版本的代码

val resourceVersionPattern: Pattern = Pattern.compile("(?<=version=).*")

def getResourceVersion(acceptHeader: String): String = {
            import java.util.regex.Matcher
            val matcher: Matcher = resourceVersionPattern.matcher(acceptHeader)
            if(matcher.find()) ("v" + matcher.group(1)).trim() else "v1.0"
    }

When I am invoking the above function which is intended to extract version (for example can be of the form v1.0 or v1.5 or v2.5)

当我调用上述用于提取版本的函数时(例如可以采用 v1.0 或 v1.5 或 v2.5 的形式)

 getResourceVersion("vnd.example-com.foo+json; version=1.1")

I get following exception:

我得到以下异常:

java.lang.IndexOutOfBoundsException: No group 1
at java.util.regex.Matcher.group(Matcher.java:487)
at .getResourceVersion(<console>:12)
at .<init>(<console>:11)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon.run(package.scala:25)
at java.lang.Thread.run(Thread.java:744)

I think I am doing something wrong in my regex or the input string has some illegal charecters which I am not able to identify with my limited knowledge of Regular expression. Help me find out the reason.

我想我在正则表达式中做错了什么,或者输入字符串有一些非法字符,我无法用我对正则表达式的有限知识来识别这些字符。帮我找出原因。

采纳答案by nhahtdh

It'd advise against (ab)using look-behind, if it is possible to write one without that is clear and does the same thing.

它建议不要(ab)使用后视,如果有可能在没有它的情况下编写一个清晰的并且做同样的事情。

Just use the pattern:

只需使用模式:

version=(.*)

And what you want will be in capturing group 1.

而您想要的将是捕获第 1 组。

回答by falsetru

The code is using lookbehind assertion:

代码使用了后视断言:

(?<=version=)

That is not captured as a group. If you want capture version=as a group, use capturing group:

这不是作为一个组捕获的。如果要将捕获version=作为一个组,请使用捕获组:

(version=)

To get 1.1from the given string input, use following regular expression:

要从1.1给定的字符串输入中获取,请使用以下正则表达式:

(?<=version=)(.*)

回答by Alex Wittig

Surround the part you want to capture with parentheses to create a group (Actually, this is optional in your case since you want the entire matching area):

用括号包围要捕获的部分以创建一个组(实际上,这在您的情况下是可选的,因为您需要整个匹配区域):

Pattern.compile("(?<=version=)(.*)")

Then use matcher.group()(with no parameter) to return 1.1for your example input.

然后使用matcher.group()(不带参数)返回1.1您的示例输入。

Note that you must call matcher.find()before using matcher.group().

请注意,您必须matcher.find()在使用之前调用matcher.group()

matcher.group(x)will only work if your regexp matches the entireinput string.

matcher.group(x)仅当您的正则表达式匹配整个输入字符串时才有效。

回答by dukemalik

This is the code u r looking for...

这是您正在寻找的代码...

void versionFinder() {

无效版本查找器(){

    String version = "vnd.example-com.foo+json; version=1.1";
    String regex = "(?<=(version=))(\d((\.)(\d)+)?)";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(version);
    int i=0;

    while (m.find()) {

        System.out.println(i+"-----------"+m.group() +"-------"+m.start());
        int j = m.start();
        String printable ="";
        while(j!=version.length() && version.charAt(j)!=' ')
                    {
            printable+=version.charAt(j);
            j++;
        }
        printable="v"+printable;
        System.out.println(printable);
        i++;
    }

}

}

You can change the regex after \d as per ur need.

您可以根据需要在 \d 之后更改正则表达式。