How to regex dashes and special characters?

I am looking to eliminate dashes in the following text:

Century Surety Company - Commercial Ocean Marine Declarations — Marine Liability

I would like to remove both types of dashes - and —

You don’t need to escape the dashes (usually). Other special characters, e.g. $ you need to escape with backslash: \$

[-—]+

The dash - is a meta character if not in the beginning or at the end of a character class, e.g. [0-9] will match any digits between 0 and 9. If you only want to match 0, dash or 9, you need to escape the dash: [0\-9]

1 Like

thank you! this worked. is there any way I could eliminate single spaces of white space in between the words with regex with that?

Century Surety Company Commercial Ocean Marine Declarations Marine Liability

You are welcome! Glad to hear that it’s working. To remove the double spaces, use the pattern below with Regex.Replace().

newString = System.Text.RegularExpressions.Regex.Replace(inputString, " +[-—]+ +", " ")

image

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