Existe-t-il un moyen de remplacer une expression rationnelle par le contenu modifié du groupe de capture?Java Regex Remplacer par le groupe de capture
Exemple:
Pattern regex = Pattern.compile("(\\d{1,2})");
Matcher regexMatcher = regex.matcher(text);
resultString = regexMatcher.replaceAll("$1"); // *3 ??
Et je voudrais remplacer tout événement avec 1 $ multiplié par 3.
modifier:
On dirait, quelque chose qui ne va pas :(
Si j'utilise
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();
}
Il lance une IllegalStateException: Aucun résultat trouvé
Mais
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();
}
fonctionne très bien, mais je ne peux pas changer le 1 :(
$ modifier:
maintenant , ça marche :)
en travaillant directement sur une chaîne, nous avons enfin [ce] (http: // stackoverflow .com/a/6057820/1422630) –