4

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?

0

3 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

mmh no with that I only get Exception in thread "main" java.lang.IllegalStateException: No match available
Oh, well, don't forget to call matcher.find() to actually look for your regex.
2

It seems to me that you could 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!"

Comments

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

output : testline to test the matcher!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.