Regex to replace one single character

Hi friends,

want to replace one single character in a string using regex. Could anyone help on this?

E.g Mark S Twain==> S to be replaced as string.empty and result to be “Mark Twain”

J Mark Tailor ==> Again J to be replaced as string.empty and result to be “Mark Tailor”

Thanks,
Ula

1 Like

Hi
Hope this expression would help you resolve this
If this string is in a variable of type string named strinput

Then for specific to this string input
Stroutput = System.Text.RegularExpressions.Regex.Replace(strinput.ToString,”( S )”,” ”).ToString

Or for general string input with this kind character in between or atlast

Stroutput = System.Text.RegularExpressions.Regex.Replace(strinput.ToString,”\s[A-Z]{1,3}\s”).ToString

Cheers

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Replace(strData,"(^[A-Z] )|((?<= )[A-Z] )","")

Regards,

2 Likes

Hi @Yoichi

It is not highlighting any single letter character.

image

Thanks

Hi @Palaniyappan

Thank you. It is not highlighting any single character. Could you please check and help me on this?

Regular exp need to highlight any single character in a group of words
h kjlkdf werwer - only h to highlighted using regex and then i replace it with string.empty
iuoiuower piupwer k - only k to be highlighted and replaced.

uouoewr r oopoiwer - only r to be highlighted and replaced.

Thanks

1 Like

If the single character is always capital and followed by a space, you can use the following regular expression. Please mention if you have any other specific requirement.

[A-Z]{1}(\s)

1 Like

Awesome got it same expression but with only one correction

This worked perfect as per your requirement When I tried to simulate
Kindly have a view on this

Cheers @Boopathi

Hi,

If you want to remove not only upper case but also lower case, please use [A-Za-z] instead of [A-Z].

Regards,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.