Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ready-made character class and restrict it further

Lots of ready-to-use character classes are available in Perl regular expressions, such as \d or \S, or new-fangled Unicode grokkers such as \p{P}, which matches punctuation characters.

Now let's say I'd like to match all punctuation characters \p{P} (quite a number of them, and not something you want to type in by hand) - all but one, all but the good old komma (or comma, ,).

Is there a way to specify this requirement short of expanding the handy character class and taking away the komma by hand?

like image 932
Lumi Avatar asked Dec 14 '11 11:12

Lumi


People also ask

What is character class in regex?

In the context of regular expressions, a character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.

What is a whitespace character in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

What is character class in Javascript?

A character class is a special notation that matches any symbol from a certain set. For the start, let's explore the “digit” class. It's written as \d and corresponds to “any single digit”. For instance, let's find the first digit in the phone number: let str = "+7(903)-123-45-67"; let regexp = /\d/; alert( str.

What is the dot or period character used for in Javascript?

The dot( . ) matches any character except the newline character. Use the s flag to make the dot ( . ) character class matches any character including the newline.


1 Answers

$ unichars -au '\p{P}' | wc -l
598

Double negation:

/[^\P{P},]/

$ unichars -au '[^\P{P},]' | wc -l
597

"And" through lookahead/lookbehind:

/\p{P}(?<!,)/

$ unichars -au '\p{P}(?<!,)' | wc -l
597

unichars

like image 172
ikegami Avatar answered Nov 12 '22 16:11

ikegami