regex - java.lang.IndexOutOfBoundsException: No group 1 | Pattern matching -
i trying extract value of version accept
header can of form
"vnd.example-com.foo+json; version=1.1"
here code extracting 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 invoking above function intended extract version (for example can of form v1.0 or v1.5 or v2.5)
getresourceversion("vnd.example-com.foo+json; version=1.1")
i following exception:
java.lang.indexoutofboundsexception: no group 1 @ java.util.regex.matcher.group(matcher.java:487) @ .getresourceversion(<console>:12) @ .<init>(<console>:11) @ .<clinit>(<console>) @ .<init>(<console>:11) @ .<clinit>(<console>) @ $print(<console>) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ scala.tools.nsc.interpreter.imain$readevalprint.call(imain.scala:704) @ scala.tools.nsc.interpreter.imain$request$$anonfun$14.apply(imain.scala:920) @ scala.tools.nsc.interpreter.line$$anonfun$1.apply$mcv$sp(line.scala:43) @ scala.tools.nsc.io.package$$anon$2.run(package.scala:25) @ java.lang.thread.run(thread.java:744)
i think doing wrong in regex or input string has illegal charecters not able identify limited knowledge of regular expression. me find out reason.
it'd advise against (ab)using look-behind, if possible write 1 without clear , same thing.
just use pattern:
version=(.*)
and want in capturing group 1.
Comments
Post a Comment