Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Regular Expression For "Not Whitespace and Not a hyphen"

Tags:

regex

I tried this but it doesn't work :

[^\s-] 

Any Ideas?

like image 749
rudimenter Avatar asked May 07 '10 11:05

rudimenter


People also ask

What is a non-whitespace character in regex?

Non-whitespace character: \S.

What is the regex for hyphen?

The quantifier notations In regular expressions, the hyphen ("-") notation has special meaning; it indicates a range that would match any number from 0 to 9. As a result, you must escape the "-" character with a forward slash ("\") when matching the literal hyphens in a social security number.

Do not include space in regex?

A regex to match a string that does not contain only whitespace characters can be written in two ways. The first involves using wildcards (.) and the non-whitespace character set (\S), while the other one involves a positive lookahead to check that the string contains at least one non-whitespace character (\S).

What is whitespace and non-whitespace?

A whitespace character is a space (U+0020), tab (U+0009), newline (U+000A), line tabulation (U+000B), form feed (U+000C), or carriage return (U+000D). A space is U+0020. A non-space character is any character that is not a whitespace character.


1 Answers

[^\s-] 

should work and so will

[^-\s] 
  • [] : The char class
  • ^ : Inside the char class ^ is the negator when it appears in the beginning.
  • \s : short for a white space
  • - : a literal hyphen. A hyphen is a meta char inside a char class but not when it appears in the beginning or at the end.
like image 116
codaddict Avatar answered Oct 16 '22 18:10

codaddict