Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用于多行字符串的ASP.Net MVC正则表达式_Regex_Asp.net Mvc_Validation - Fatal编程技术网

Regex 用于多行字符串的ASP.Net MVC正则表达式

Regex 用于多行字符串的ASP.Net MVC正则表达式,regex,asp.net-mvc,validation,Regex,Asp.net Mvc,Validation,我只需要在多行文本区域输入字段中允许以下字符: 空格、数字、英文字母和以下特殊字符:!“#$%&'()*+,-./:;?@`[\~]^{124}” 除了多线部分外,我还算出了: [RegularExpression("^[ -~]+$", ErrorMessage = @"Allowed characters for item description: space, numbers, English letters and following special characters

我只需要在多行文本区域输入字段中允许以下字符:

空格、数字、英文字母和以下特殊字符:!“#$%&'()*+,-./:;<=>?@`[\~]^{124}”

除了多线部分外,我还算出了:

        [RegularExpression("^[ -~]+$", ErrorMessage = @"Allowed characters for item description: space, numbers, English letters and following special characters: ! "" # $ % & ' ( ) * + , - . / : ; < = > ? @ ` [ \ ~ ] ^ _ {{ | }}")]
[RegularExpression(“^[-~]+$”,ErrorMessage=@“允许项目描述使用的字符:空格、数字、英文字母和以下特殊字符:!”“\$%&'()*+,-./:;<=>?@`[\~]^{{124}”)]
只要所有内容都作为一行输入,它就可以正常工作。我只是不知道如何将它扩展到多行。

您可以使用

"^[ -~]*(?:\r?\n[ -~]*)*$"
模式匹配:

  • ^
    -字符串的开头
  • [-~]*
    -0或更多可打印的ASCII字符
  • (?:
    -开始非捕获组匹配
    • \r?\n
      -可选的(1或0)CR符号,然后是LF符号(以便它与Windows和Unix/Linux行结束符匹配)
    • [-~]*
      -0或更多可打印的ASCII字符
  • )*
    -…零次或多次
  • $
    -字符串结束

测试值是否包含多行?请尝试
“^[-~+(?:\r?\n[-~+)*$”
@WiktorStribiżew:几乎就是这样。除非第一行或最后一行是空的,否则它可以工作line@JoeSchmoe这是什么意思?我的行应该是空的?然后使用
“^[-~]*(?:\r?\n[-~]*)*$“
@WiktorStribiżew:您的第二个版本似乎100%正常工作。我之前的意思是:如果我输入“ABC[enter]DEF[enter]”,那么它不会显示,但现在它显示了。如果你将此作为答案发布,我会将其标记为答案。