how do i pull the "16" out for both
Here is what i have tried
String V,Line ="Bar Foo Bar: Foo8:16 Foo Bar Bar foo barz";
V = Line.substring(Line.indexOf("([0-9]+:[0-9]+)+")+1);
V = V.substring(V.indexOf(":")+1, V.indexOf(" "));
System.out.println(V);
And here is the error i get
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -9
at java.lang.String.substring(String.java:1955)
at Indexing.Index(Indexing.java:94)
at Indexing.main(Indexing.java:24)
I tested the regex("([0-9]+:[0-9]+)+") at http://regexr.com/ and it correctly highlight the "8:16"
You need to place the capturing group on the second [0-9]+
(or an equivalent, \d+
) and use a Matcher#find()
:
String value1 = "Bar Foo Bar: Foo8:16 Foo Bar Bar foo barz";
String pattern1 = "\\d+:(\\d+)"; // <= The first group is the \d+ in round brackets
Pattern ptrn = Pattern.compile(pattern1);
Matcher matcher = ptrn.matcher(value1);
if (matcher.find())
System.out.println(matcher.group(1)); // <= Print the value captured by the first group
else
System.out.println("false");
See demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With