Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent of Java Punctuation regex

Tags:

java

c#

regex

I'm looking to find the equivalent in C# for the equivalent of this regex.

Java:

public static final String expression = "[\\s\\p{Punct}]";

{Punct} is a reserved character class in Java but I'm not sure how to create the equivalent expression so that the .net regex engine doesn't barf.

like image 308
user325099 Avatar asked Jun 11 '26 02:06

user325099


2 Answers

[\s\p{P}] matches all whitespace and punctuation. Amusingly enough, it can be found in this exact form as an example in the MSDN documentation on Character Classes. As in Java, \p{x} is used for any single character from unicode category x. See the part on Unicode Categories for a list of the possibilities other than P.

like image 198
Joren Avatar answered Jun 12 '26 14:06

Joren


Use this:

Regex regex = new Regex(@"[\s\p{P}]");

Note in particular the use of @.

like image 34
Mark Byers Avatar answered Jun 12 '26 16:06

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!