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?

4

3 に答える 3

2

単純に使用できるように思えます.replaceFirst("\\d+:\\d+", "")

String line = "4:12testline to test the matcher!";
System.out.println(line.replaceFirst("\\d+:\\d+", ""));
// prints "testline to test the matcher!"
于 2013-09-02T14:06:42.193 に答える
1
String line = "4:12testline to test the matcher!";
line = line.replaceFirst("\\d{1,2}:\\d{1,2}", "");
System.out.println(line);

output : マッチャーをテストするためのテストライン!

于 2013-09-02T13:57:34.780 に答える