I would like to remove a single letter word from a string. For example,
the string is - string1 = “United Parcel E Service” Output string needs to be - “United Parcel Service”
(the letter ‘e’ must stay in ‘United’, ‘Parcel’ and ‘Service’ and must not be removed)
Another example would be string2 = “MARTIN N EVANS” Output needs to be - “MARTIN EVANS”
Thank you for your reply. Yes it works for a single letter between a string. But this regex expression does not work for a single letter that is at the end of the string.
For example as similar as i had mentioned,
string1 = “United Parcel Service E”, the output needs to be “United Parcel Service”. The regex does not seem to work for this condition.
So the single letter which stands alone can be anywhere in the string, at the beginning or middle or at the end. I would like to remove that only.
What if you change "\b\D\s\b" to use an ‘or’
something like this pattern: "(?<=(^|\s))\D(\s|$)"
I used “^|\s” so it looks for either the start of the string or a whitespace. “?<=” is a look behind so it only matches with the space after the letter (so when you replace it with “” it won’t remove the space. Then, used “\s|$” so it matches either a whitespace or the end of the line after the character.