正则表达式限制带有换行符的字符串的字符串长度 [英] Regex to limit string length for strings with new line characters
问题描述
看起来像一个简单的任务-获得一个正则表达式,用于测试特定长度的字符串:^.{1,500} $
Looks like a simple task - get a regex that tests a string for particular length: ^.{1,500}$
但是,如果字符串中包含"\ r \ n",则上述匹配总是失败!
But if a string has "\r\n" than the above match always fails!
正确的正则表达式应该如何接受新的换行字符作为字符串的一部分?
How should the correct regex look like to accept new line characters as part of the string?
我有一个< asp:TextBox TextMode ="Multiline">
,并使用RegularExpressionValidator来检查用户键入内容的长度.
I have a <asp:TextBox TextMode="Multiline">
and use a RegularExpressionValidator to check the length of what user types in.
谢谢你,安德烈
推荐答案
验证输入时,可以使用 RegexOptions.Singleline
选项.它将输入视为单行语句,并按如下方式对其进行解析.
You could use the RegexOptions.Singleline
option when validating input. This treats the input as a single line statement, and parses it as such.
否则,您可以尝试以下表达式:
Otherwise you could give the following expression a try:
^(.|\s){1,500}$
这应该在多行输入中起作用.
This should work in multiline inputs.
这篇关于正则表达式限制带有换行符的字符串的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!