Is there an easy way to match all punctuation except period and underscore, in a C# regex? Hoping to do it without enumerating every single punctuation mark.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use Regex Subtraction
See the .NET Regex documentation. I’m not sure if other flavors support it.
C# example
Explanation
The pattern is a Character Class Subtraction. It starts with a standard character class like
[\p{P}]and then adds a Subtraction Character Class like-[._], which says to remove the.and_. The subtraction is placed inside the[ ]after the standard class guts.