Java scope regexEnd

I am trying to write a regular expression that finds the time at the beginning of a line. I want to remove this part of the line, so I use the regioEnd function, but it sets 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?

+4
source share
3 answers
 String line = "4:12testline to test the matcher!"; line = line.replaceFirst("\\d{1,2}:\\d{1,2}", ""); System.out.println(line); 

Conclusion: a test line to verify compliance!

+1
source

It looks like you want to use only end() instead of regionEnd() . The end() method returns the index of the first character at the end of what actually matches the regular expression. The regionEnd() method returns the end of the region where the match is looking for a match (which you can set).

+4
source

It seems to me that you can simply use .replaceFirst("\\d+:\\d+", "") :

 String line = "4:12testline to test the matcher!"; System.out.println(line.replaceFirst("\\d+:\\d+", "")); // prints "testline to test the matcher!" 
+2
source

Source: https://habr.com/ru/post/1500008/


All Articles