Regular expression of minimum length

I am trying to write a regex that will verify that user input is greater than X number of characters without spaces. I mainly try to filter out leading and trailing spaces, while preserving that the input is larger than the X characters; characters can be any, but not spaces (space, tab, return, new line). This is the regex that I used, but it doesn't work:

\s.{10}.*\s 

I use C # 4.0 (Validator Validation Validator) if that matters.

+6
source share
3 answers

It may be easier to not use a regex at all:

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

If the space should not be counted in the middle, this will work:

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

If you care about spaces between characters without spaces, other answers have this scenario.

+7
source

This regular expression searches for eight or more characters between the first and last characters without spaces, ignoring leading and trailing spaces:

 \s*\S.{8,}\S\s* 
+3
source

If you are trying to check (for example, in my case, a phone number that contains 8 digits), you need to refer to the number below the one you need.

 (\s*(\S)\s*){7,} 
+1
source

Source: https://habr.com/ru/post/906996/


All Articles