3

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user325099
  • 31
  • 3

2 Answers2

3

[\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.

Joren
  • 14,472
  • 3
  • 50
  • 54
  • Heh... I came up with the exact same solution as you, although it is worth pointing out that this won't work like that. You need to use `@` before the string or else you need to escape the backslashes. – Mark Byers Apr 24 '10 at 20:32
  • Yeah, I thought of that, so I just edited out the quote marks. Now my post refers to the literal string values; how you denote these values in C# is a different matter. ;-) By the way, isn't the string in the question technically incorrect as well, for the same reason? – Joren Apr 24 '10 at 20:35
  • If you click edit you can see he wrote `"[\\s\\p{Punct}]"` but it formatted incorrectly. I'll edit the question so you can see what he intended to write. – Mark Byers Apr 24 '10 at 20:38
  • Yeah, this is what I've found too. Thanks for your responses! – user325099 Apr 25 '10 at 20:42
3

Use this:

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

Note in particular the use of @.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452