Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use \p{Punct} in a regex(java), but without the "(",")" characters?

Tags:

java

regex

Is there a way to use \p{Punct} in a regex in java, but without the two characters ( and ) ?

like image 913
tomkpunkt Avatar asked Jun 08 '11 13:06

tomkpunkt


People also ask

What is P Punct in Java?

PreviousNext. The character class \p{Punct} matches any punctuation character. The following example shows the usage of Posix character class matching.

What does \\ mean in Java regex?

The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.

How do you add a space in a pattern in Java?

The Difference Between \s and \s+ For example, expression X+ matches one or more X characters. Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.


1 Answers

You should be able to use:

[\p{Punct}&&[^()]]

What this is saying is:

The punct character class except for ( and ).

The ^ character specifies a negative character class. The && is an intersection between the punct class and the custom class for the parenthesis.

Have a look at the Pattern Javadocs for more info.

like image 148
jjnguy Avatar answered Oct 06 '22 00:10

jjnguy