Reputation: 1685
I want to write a regular expression that removes whitespace between initials.
I figured out this expression:
(\w\.\s){2,}
Which will match only occurrences of 2 or more initials:
But I am stuck on how to match ONLY the whitespace within a group of 2 or more initials (except there should still be a space between last name and initials.)
When I remove the spaces, the expected result is that strings will change in the following way:
J. I. Howard --> J.I. Howard
J. Howard --> No change
J.I. Howard -> No change
J. O. B. Franklin --> J.O.B. Franklin
I think capture groups are probably involved somewhere. Any help is appreciated.
Edit: My only other issue is Excluding Mr. and Mrs. from the initial group.
For example:
Mr. L. P. Johnson --> Mr. L.P. Johnson
Mrs. C. Dalloway --> No change
Upvotes: 0
Views: 56
Reputation: 75860
It seems as though you can use either a lookbehind in combination with a lookahead:
(?<=[A-Z]\.) (?=[A-Z]\.)
Replace with empty string. See an online demo
Or, capture preceding initial in a capture group and use a backreference in the replacement:
\b([A-Z]\.) (?=[A-Z]\.)
See an online demo.
Upvotes: 2