I am trying to write something like this:
Pattern p = Pattern.compile("Mar\\w");
Matcher m = p.matcher("Mary");
String result = m.replaceAll("\\w");
The result would ideally be "y". Any ideas?
Your question is not so clear, but I think you want to use a lookahead:
Pattern p = Pattern.compile("Mar(?=\\w)");
Matcher m = p.matcher("Mary");
String result = m.replaceAll("");
See it online: ideone
Or you could use a capturing group:
Pattern p = Pattern.compile("Mar(\\w)");
Matcher m = p.matcher("Mary");
String result = m.replaceAll("$1");
See it online: ideone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With