简体   繁体   English

最小长度正则表达式

[英]Minimum Length Regular Expression

I'm trying to write a regular expression that will validate that user input is greater than X number of non-whitespace characters. 我正在尝试编写一个正则表达式来验证用户输入是否大于X个非空白字符。 I'm basically trying to filter out begining and ending whitespace while still ensuring that the input is greater than X characters; 我基本上试图过滤掉开始和结束空格,同时仍然确保输入大于X个字符; 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. 我正在使用C#4.0(Asp.net正则表达式验证器),如果这很重要的话。

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. 如果您不关心非空白字符之间的空格,则其他答案会涵盖该方案。

此正则表达式在第一个和最后一个非空白字符之间查找八个或更多字符,忽略前导和尾随空格:

\s*\S.{8,}\S\s*

如果您尝试检查(就像我的情况下包含8位数字的电话号码),您需要参考您需要的号码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM