Reputation: 973
I have a string. Let's say:
String s = "This is my P.C.. My P.C. is the best.O.M.G!! Check this...";
I want to replace all the P.C.
to PC
words and O.M.G
to OMG
. In general, I want to replace all the dots that are between single letters OR single letter and a space or dot. I think the matching regex for that is:
[^A-Za-z][A-Za-z]\\.[A-Za-z\\s\\.][^A-Za-z]
How can I replace only the dot from that and not everything that matches?
EDIT:
Expected output:
"This is my PC. My PC is the best.OMG!! Check this..."
EDIT2:
The basic task is to remove dots from abbreviations and acronyms that could be written with or without dots. So a good regex is also valuable
Upvotes: 1
Views: 6748
Reputation: 48404
Use "lookarounds" for this.
The Pattern
below replaces dots only if preceded and followed by a capital letter.
A second iteration is recommended if you are picky and want to sanitize the ..
into .
without affecting the suspension ...
.
String s = "This is my P.C.. My P.C. is the best.O.M.G!! Check this...";
// | preceded by capital letter
// | | escaped dot
// | | | followed by capital letter
// | | | | replace with empty String
System.out.println(s.replaceAll("(?<=[A-Z])\\.(?=[A-Z])", "")
// second replacement: ".." folllowed by whitespace is "sanitized" with only 1 dot
.replaceAll("\\.{2}(?=\\s)", "."));
Output
This is my PC. My PC. is the best.OMG!! Check this...
Upvotes: 0
Reputation: 70732
You may consider using Positive Lookahead to assert what follows is either a letter, dot .
or space.
String s = "This is my P.C.. My P.C. is the best.O.M.G!! Check this...";
String r = s.replaceAll("([A-Z])\\.(?=[ A-Z.])", "$1");
System.out.println(r); //=> "This is my PC. My PC is the best.OMG!! Check this..."
Upvotes: 2
Reputation: 67968
(?<=[A-Z])(\.)(?=[A-Z]|\.|\s)
You can try this.
See Demo.
http://regex101.com/r/lK9iD2/2
Upvotes: 0
Reputation: 174706
You could use the below regex which uses positive lookbehind to remove all the dots which are present just after to the Uppercase letters,
System.out.println("This is my P.C.. My P.C. is the best.O.M.G!! Check this...".replaceAll("(?<=[A-Z])\\.", ""));
Output:
This is my PC. My PC is the best.OMG!! Check this...
Upvotes: 0