最小长度正则表达式 [英] Minimum Length Regular Expression
问题描述
我想写一个正则表达式将会验证用户输入比非空白字符X数字更大。我基本上是试图筛选出开始时和结束的空白,同时还确保输入超过X个字符;字符可以是任何东西,只是没有空格(空格,制表符,换行符)。
这是我一直在使用正则表达式,但它不工作:
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
我使用C#4.0(Asp.net正则表达式验证)BTW 。如果该事项
I'm using C# 4.0 (Asp.net Regular Expression Validator) btw if that matters.
推荐答案
这可能是更容易不使用正则表达式都:
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.
这篇关于最小长度正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!