Libin Joseph
Libin Joseph

Reputation: 7342

Regular expression for any characters without spaces

What is the regular expression for all characters except white space , and minimum6 characters.

This is what I have now :

^[\w'?@&#.]{6,}$

But this does not accept all the special characters. And I am using in .net app if that makes any difference

Upvotes: 1

Views: 4168

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

A .NET regex to match any string that does not contain any whitespace chars (at least 6 occurrences) is

\A\S{6,}\z

See the regex demo online

Do not use $ because it may match before a final \n (LF symbol) inside a string, \z is the most appropriate anchor here as it matches the very end of the string. To make the string compatible with JS (if you use it in ASP.NET for both server and client side validation) you need to use ^\S{6,}$(?!\n).

The \S shorthand character class matches any character other than a Unicode whitespace char (if ECMAScript option is not used).

The {6,} limiting quantifier matches six or more occurrences of the quantified subpattern.

Details

  • \A - (an unambiguous anchor, its behavior cannot be redefined with any regex options) start of a string
  • \S{6,} - any 6 or more chars other than a Unicode whitespace char
  • \z - the very end of the string.

Upvotes: 0

BuBa Gomes
BuBa Gomes

Reputation: 46

[^\s]{6,}$ should make it. But note the answer above, if you only want to skip the white spaces, you better use [^ ]. The notation [^\s] will ignore any white space character (space, tab or newline).

Upvotes: 3

Related Questions