H
Henner Graubitz
Hi all,
is there a nice solution to show what string has been replaced by
java.util.regex?
For example: following code:
String replacement = "$1.01.$3";
Pattern p = Pattern.compile("([0-9]{1,2})\\.[\\ ]*(Januar|Jan[\\.]?)[\\
]*([\\d]{2,4})");
Matcher m = p.matcher("one cat on 18. Januar 1974 and two cats on 1.
Januar 1871 has been hidden in the yard");
String newString = m.replaceAll(replacement);
while (m.find()) {
System.out.println("Start -> " + m.start()); // returns start
position of the OLD String
System.out.println("End -> " + m.end()); // returns end
position of the OLD String
System.out.println("Group -> " + m.group()); // returns the match
in the OLD String
counter ++;
}
System.out.println("newString -> " + newString); // returns the new
string
---> The output looks like
Start -> 11
End -> 26
Group -> 18. Januar 1974
Start -> 40
End -> 54
Group -> 1. Januar 1871
newString -> one cat am 18.01.1974 two sowie am 1.01.1871 cats in the yard
That is ok, but WHAT I WANT IS THE REPLACEMENT
a) 18.01.1974 and b) 1.01.1871
Is there any nice solutuion ?
Thanks for any advice
Henner Graubitz
is there a nice solution to show what string has been replaced by
java.util.regex?
For example: following code:
String replacement = "$1.01.$3";
Pattern p = Pattern.compile("([0-9]{1,2})\\.[\\ ]*(Januar|Jan[\\.]?)[\\
]*([\\d]{2,4})");
Matcher m = p.matcher("one cat on 18. Januar 1974 and two cats on 1.
Januar 1871 has been hidden in the yard");
String newString = m.replaceAll(replacement);
while (m.find()) {
System.out.println("Start -> " + m.start()); // returns start
position of the OLD String
System.out.println("End -> " + m.end()); // returns end
position of the OLD String
System.out.println("Group -> " + m.group()); // returns the match
in the OLD String
counter ++;
}
System.out.println("newString -> " + newString); // returns the new
string
---> The output looks like
Start -> 11
End -> 26
Group -> 18. Januar 1974
Start -> 40
End -> 54
Group -> 1. Januar 1871
newString -> one cat am 18.01.1974 two sowie am 1.01.1871 cats in the yard
That is ok, but WHAT I WANT IS THE REPLACEMENT
a) 18.01.1974 and b) 1.01.1871
Is there any nice solutuion ?
Thanks for any advice
Henner Graubitz