简体   繁体   English

正则表达式

[英]Regular expression

I have an aspx:memo control in asp.net page.我在 asp.net 页面中有一个 aspx:memo 控件。 The control has a regular expression validator.该控件有一个正则表达式验证器。 If the text inserted in the memo is different than the regular expression then the validator triggers an error.如果备忘录中插入的文本与正则表达式不同,则验证器会触发错误。 So I would like to accept every character except these:所以我想接受除这些之外的每个角色:

1) -- (double hyphen)
2) // (double slash)
3) ' (Single quote)
4) \\ (double backslash)
5) ^ (Caret)
6) ; (Semicolon)

So far I have created this expression:到目前为止,我已经创建了这个表达式:

^[\na-zA-Z0-9 .,~?`~!@():#&%=+΄<>\\\-\/_&quot;\]\[\}\{]*$

I have put inside every character that I accept in the memo.我已经把我在备忘录中接受的每个字符都放进去了。 So the issue is that with this expression I accept slashes and backslashes or hyphens even if they are single or more.所以问题是,对于这个表达式,我接受斜杠和反斜杠或连字符,即使它们是单个或多个。 How can I disallow the double slashes or hyphens but allow single ones.如何禁止双斜线或连字符但允许单斜线。

I have already lost a lot of time for this.我已经为此失去了很多时间。 Your help would be greatly appreciated.您的帮助将不胜感激。

Thank you in advance.先感谢您。

^((?!--|\\/\\/|'|\\\\\\\\|\\^|;).|\\w)*$

Breakdown分解

(?! ) being the negative look ahead meaning that anything within (separated by | ) will cause the validation to fail (?! )是负面展望,这意味着(由|分隔)内的任何内容都将导致验证失败

\\/\\/ being // with escaped characters \\/\\/正在//带有转义字符

\\\\\\\\ being \\\\ with escaped characters \\\\\\\\\\\\带有转义字符

. allows any character (except whitespace)允许任何字符(空格除外)

\\w allows whitespace \\w允许空格

Try this one:试试这个:

(?<!\-)(?<!\\)(?<!\/)[^;\^'](?!\-)(?!\\)(?!\/)

Explanation解释

(?<!\\-)(?<!\\\\)(?<!\\/) => Do not match \\ or / or ' before the blacklist. (?<!\\-)(?<!\\\\)(?<!\\/) => 不要匹配黑名单前的\\/'

[^;\\^'] => Our blacklist. [^;\\^'] => 我们的黑名单。 Do not match ;不匹配; or ' or ^ , match everything else.'^ ,匹配其他所有内容。

(?!\\-)(?!\\\\)(?!\\/) => Dot not match \\ or / or ' after the blacklist. (?!\\-)(?!\\\\)(?!\\/) => 在黑名单后不要匹配\\/'

So we allow everything but ;所以我们允许一切,但; and ' .' But only, if there is no \\ or / or ' in front or after the character.但仅限于在字符前面或后面没有\\/'的情况下。 This means single occurances of these characters are allowed.这意味着这些字符只出现一次是允许的。

EDIT:编辑:

This matches only single characters.这仅匹配单个字符。 Better use the accepted answer.更好地使用已接受的答案。

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

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