I have an issue where I need to use a RegularExpressionValidator
to limit the length of a string to 400 Characters.
我有一个问题,我需要使用一个RegularExpressionValidator来限制一个字符串的长度为400个字符。
My expression was .{0,400}
我的表达是{ 0400 }。
My question: Is there a way to limit the length of characters to 400 without taking into consideration blank spaces?
我的问题是:有没有一种方法可以在不考虑空格的情况下将字符长度限制在400个?
I want to be able to accept blank spaces in the string but not count it in the length. Is this possible?
我希望能够接受字符串中的空格,但不以长度计算。这是可能的吗?
2 个解决方案
#1
11
I pretty much agree with Greg, but here's the regex you want:
我非常同意格雷格的观点,但这是你想要的regex:
^\s*([^\s]\s*){0,400}$
@Boopid: If you really meant only the space character, replace \s with a space in the regex.
@Boopid:如果你真的只指空格字符,用regex中的空格替换\s。
#2
7
It sounds like you might want to write your own validator class instead of using the RegularExpressionValidator. Regular expressions certainly have their uses, but this doesn't sound like one of them.
听起来您可能想编写自己的验证器类,而不是使用正则表达式验证器。正则表达式当然有它们的用途,但这听起来不像是其中之一。
Your custom validator could remove all the spaces, then check the length of the string. Ultimately, the code will be more readable than a regular expression that does the same thing.
您的定制验证器可以删除所有空格,然后检查字符串的长度。最终,代码将比执行相同操作的正则表达式更具可读性。
#1
11
I pretty much agree with Greg, but here's the regex you want:
我非常同意格雷格的观点,但这是你想要的regex:
^\s*([^\s]\s*){0,400}$
@Boopid: If you really meant only the space character, replace \s with a space in the regex.
@Boopid:如果你真的只指空格字符,用regex中的空格替换\s。
#2
7
It sounds like you might want to write your own validator class instead of using the RegularExpressionValidator. Regular expressions certainly have their uses, but this doesn't sound like one of them.
听起来您可能想编写自己的验证器类,而不是使用正则表达式验证器。正则表达式当然有它们的用途,但这听起来不像是其中之一。
Your custom validator could remove all the spaces, then check the length of the string. Ultimately, the code will be more readable than a regular expression that does the same thing.
您的定制验证器可以删除所有空格,然后检查字符串的长度。最终,代码将比执行相同操作的正则表达式更具可读性。