Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex regionEnd

Tags:

java

regex

I'm trying to write a regex that finds times at the beginning of a string. I want to remove that part of the string so I'm using the regioEnd function but it sets the regioend to the end of the line.

Pattern pattern = Pattern.compile("\\d+.\\d+");
String line = "4:12testline to test the matcher!";
System.out.println(line);
Matcher matcher = pattern.matcher(line);
int index = matcher.regionEnd();
System.out.println(line.substring(index));

What am I doing wrong here?

like image 998
ramin Avatar asked Mar 22 '23 14:03

ramin


1 Answers

It looks like you want to use just end() instead of regionEnd(). The end() method returns the index of the first character past the end of whatever the regular expression actually matched. The regionEnd() method returns the end of the area where the matcher searches for a match (which is something you can set).

like image 116
Greg Hewgill Avatar answered Apr 01 '23 19:04

Greg Hewgill