I am trying to slash out pattern as specified using regex , but in replacement also replaces wanted character . specifying boundary does not help in this case .
String name = "Dr.Dre" ;
Pattern p = Pattern.compile("(Mr.|MR.|Dr.|mr.|DR.|dr.|ms.|Ms.|MS.|Miss.|Mrs.|mrs.|miss.|MR|mr|Mr|Dr|DR|dr|ms|Ms|MS|miss|Miss|Mrs|mrs)"+"\\b");
Matcher m = p.matcher(name);
StringBuffer sb = new StringBuffer();
String namef = m.replaceAll("");
System.out.println(namef);
Input : Dr.Dre or Dr Dre or Dr. Dre
> output(expected) : Dre or Dre or Dre
Edit:
Thanks for help , but there is little regex issue I am facing:
Program:
String name = "Dr. Dre" ;
Pattern p = Pattern.compile("(Mr\\.|MR\\.|Dr\\.|mr\\.|DR\\.|dr\\.|ms\\.|Ms\\.|MS\\.|Miss\\.|Mrs\\.|mrs\\.|miss\\.|MR|mr|Mr|Dr|DR|dr|ms|Ms|MS|miss|Miss|Mrs|mrs)"+"\\b");
Matcher m = p.matcher(name);
String namef = m.replaceAll("");
System.out.println(namef);
For above program I receive output as:
. Dre
while the desired output is :
Dre
Dot in a regular expression means “any character”. You need to escape it with a backslash, which in turn needs to be escaped in a string literal:
Note that you’ll end up with a double space after removing “Dr.” from “or Dr. Dre” though…
EDIT: For some reason (I haven’t worked out why), a space after a dot doesn’t count as a word boundary. If you change your pattern to use
\\sinstead of\\b, so replace a single whitespace character, it works for “Dr. Dre” – but as noted in comments, it then fails for “Dr.Dre”. You could either remove the word boundary entirely and add a space to the later parts of the pattern (“DR |Dr |” etc) or use(\\s|\\b)which works for the cases I tried it on, but may well have other undesirable side-effects.