0

Here is my try:

Java:

public static void main(String[] args) {
 String text = "This && is **^^ a ~~@@ test.";
 System.out.println(Pattern.compile("\\p{Punct}").matcher(text).replaceAll(""));
 // OUT: This  is  a  test --> As I expected
}

C#:

static void Main(string[] args) {
 string text = "This && is **^^ a ~~@@ test.";
 Console.WriteLine(Regex.Replace(text, "\\p{P}", ""));
 // OUT: This  is ^^ a ~~ test
 // expected: This  is  a  test
 Console.ReadLine();
}

Any ideas? Thank you!

Pavel Kovalev
  • 7,521
  • 5
  • 45
  • 67
LHA
  • 9,398
  • 8
  • 46
  • 85
  • Try this [\p{P}\^] – Code Maniac Dec 03 '18 at 17:41
  • See also: [Are Java and C# regular expressions compatible?](https://stackoverflow.com/questions/538579/are-java-and-c-sharp-regular-expressions-compatible). (Spoiler alert: not necessarily...) – paulsm4 Dec 03 '18 at 19:56

1 Answers1

2

"\\p{P}" means that same in both Java and C#, i.e. match Unicode Category P (Punctuation).

Java's "\\p{Punct}" means something else, and is documented as:

Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

So, the equivalent C# is "[!\"#$%&'()*+,\\-./:;<=>?@\\[\\\\\\]^_`{|}~]"

Andreas
  • 154,647
  • 11
  • 152
  • 247