Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude some characters from a class?

Say I want to match a "word" character (\w), but exclude "_", or match a whitespace character (\s), but exclude "\t". How can I do this?

like image 953
planetp Avatar asked Aug 23 '10 15:08

planetp


People also ask

How do you negate a set of characters?

Negated Character Classes If you don't want a negated character class to match line breaks, you need to include the line break characters in the class. [^0-9\r\n] matches any character that is not a digit or a line break.

How do you negate a character in regex?

Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.


1 Answers

Use a negated class including \W or \S.

/[^\W_]/  # anything that's not a non-word character and not _ /[^\S\t]/ # anything that's not a non-space character and not \t 
like image 79
ysth Avatar answered Sep 29 '22 20:09

ysth