Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum Length Regular Expression

Tags:

c#

regex

I'm trying to write a regular expression that will validate that user input is greater than X number of non-whitespace characters. I'm basically trying to filter out begining and ending whitespace while still ensuring that the input is greater than X characters; the characters can be anything, just not whitespace (space, tab, return, newline). This the regex I've been using, but it doesn't work:

\s.{10}.*\s

I'm using C# 4.0 (Asp.net Regular Expression Validator) btw if that matters.

like image 213
Mark Avatar asked Jan 27 '12 03:01

Mark


1 Answers

It may be easier to not use regex at all:

input.Where(c => !char.IsWhiteSpace(c)).Count() > 10

If whitespace shouldn't count in the middle, then this will work:

(\s*(\S)\s*){10,}

If you don't care about whitespace in between non-whitespace characters, the other answers have that scenario covered.

like image 107
John Rasch Avatar answered Oct 06 '22 23:10

John Rasch