I have this source:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexStrings {
public static void main(String[] args) {
final String EXAMPLE_TEST = "Real bad day today XY= '5678' XY='345' yes really AYB='3456' and maybe also SA='87565' or maybe this AS = '9867' or maybe even not";
final String regex = "XY\\s*=\\s*'\\d+'";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(EXAMPLE_TEST);
System.out.println(matcher.find());
if(matcher.find()) {
System.out.println(matcher.group());
String newstring=matcher.group().replaceAll("'", "");
System.out.println(newstring);
String finalstring=EXAMPLE_TEST.replaceAll(matcher.group(), newstring);
System.out.println("ENDE:" + finalstring);
}
}
}
Without the if clause it replaces XY= '5678' with XY= 5678 and all is well, with the if clause it replaces XY='345' with XY=345. But it should only replace the one it first finds.
Any idea on how to fix it?
Thanks.
Aucun commentaire:
Enregistrer un commentaire