Czy istnieje sposób na zastąpienie wyrażeń regularnych zmodyfikowaną zawartością grupy przechwytywania?Regex Java zastępuje grupę przechwytywania
Przykład:
Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher(text);
resultString = regexMatcher.replaceAll("$1"); // *3 ??
I chciałbym, aby zastąpić wszystkie wystąpienia $ 1 pomnożona przez 3.
edit:
Wygląda na to, że coś jest źle :(
Jeśli używam
Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher("12 54 1 65");
try {
String resultString = regexMatcher.replaceAll(regexMatcher.group(1));
} catch (Exception e) {
e.printStackTrace();
}
To rzuca IllegalStateException: Nie znaleziono
Ale
Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher("12 54 1 65");
try {
String resultString = regexMatcher.replaceAll("$1");
} catch (Exception e) {
e.printStackTrace();
}
działa poprawnie, ale nie mogę zmienić $ 1 :(
EDIT:
teraz , działa :)
przez pracę bezpośrednio na sznurku, mamy wreszcie [to] (http: // stackoverflow .com/a/6057820/1422630) –