tomkpunkt
tomkpunkt

Reputation: 1403

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

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

Upvotes: 14

Views: 29404

Answers (2)

jjnguy
jjnguy

Reputation: 138932

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.

Upvotes: 34

Matt Ball
Matt Ball

Reputation: 359966

This should work:

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

&& is the intersection operator for character classes, so the intersection of \p{Punct} and [^()] is what you're after. See Character Classes.

Upvotes: 14

Related Questions