简体   繁体   English

不重复特殊字符的正则表达式(C#)

[英]Regular Expression for no repeating special characters (C#)

I am new to regular expressions and need a regular expression for address, in which user cannot enter repeating special characters such as: ..... or ,,,.../// etc and none of the special characters could be entered more than 5 times in the string. 我是正则表达式的新手,并且需要用于地址的正则表达式,其中用户无法输入重复的特殊字符,例如: .....,,,...///等,并且不能输入任何特殊字符在字符串中超过5次。

...,,,....// =>No Match
Street no. 40. hello. =>Match

Thanks in advance! 提前致谢!
I have tried this: 我已经试过了:

([a-zA-Z]+|[\s\,\.\/\-]+|[\d]+)|(\(([\da-zA-Z]|[^)^(]+){1,}\))

It selects all alphanumeric n some special character with no empty brackets. 它选择所有字母数字n以及不带方括号的特殊字符。

You can use Negative lookahead construction that asserts what is invalid to match. 您可以使用Negative lookahead construction来断言无效的匹配项。 Its format is (?! ... ) 其格式为(?!...)

For your case you can try something like this: 对于您的情况,您可以尝试如下操作:

This will not match the input string if it has 2 or more consecutive dots, commas or slashes (or any combination of them) 如果输入字符串包含2个或多个连续的点,逗号或斜杠(或它们的任意组合),则该字符串将不匹配

(?!.*[.,\/]{2}) ... rest of the regex 

This will not match the input string if it has more than 5 characters 'A'. 如果输入的字符串超过5个字符“ A”,则该字符串将与输入字符串不匹配。

(?!(.*A.*){5}) ... rest of the regex 

This will match everything except your restrictions. 除了您的限制外,这将匹配所有内容。 Repplace last part (.*) with your regex. 用正则表达式替换最后一部分(。*)。

^(?!.*[.,\/]{2})(?!(.*\..*){5})(?!(.*,.*){5})(?!(.*\/.*){5}).*$

Note: This regex may no be optimized. 注意:此正则表达式可能没有进行优化。 It may be faster if you use loop to iterate over string characters and count their occurences. 如果使用循环遍历字符串字符并计算它们的出现次数,则可能会更快。

You can use this regex: 您可以使用此正则表达式:

^(?![^,./-]*([,./-])\1)(?![^,./-]*([,./-])(?:[^,./-]*\2){4})[ \da-z,./-]+$

In C#: 在C#中:

foundMatch = Regex.IsMatch(yourString, @"^(?![^,./-]*([,./-])\1)(?![^,./-]*([,./-])(?:[^,./-]*\2){4})[ \da-z,./-]+$", RegexOptions.IgnoreCase);

Explanation 说明

  • The ^ anchor asserts that we are at the beginning of the string ^锚断言我们在字符串的开头
  • The negative lookahead (?![^,./-]*([,./-])\\1) asserts that it is not possible to match any number of special chars, followed by one special char (captured to Group 1) followed by the same special char (the \\1 backreference) 否定的超前(?![^,./-]*([,./-])\\1)断言,不可能匹配任何数量的特殊字符,后跟一个特殊字符(捕获到组1)后跟相同的特殊字符( \\1反向引用)
  • The negative lookahead (?![^,./-]*([,./-])(?:[^,./-]*\\2){4}) ` asserts that it is not possible to match any number of special chars, followed by one special char (captured to Group 2), then any non-special char and that same char from Group 2, four times (five times total) 否定的前瞻(?![^,./-]*([,./-])(?:[^,./-]*\\2){4}) `断言不可能匹配任何特殊字符数,然后是一个特殊字符(捕获到第2组),然后是任何非特殊字符以及来自第2组的相同字符,四次(总计五倍)
  • The $ anchor asserts that we are at the end of the string $锚断言我们在字符串的末尾

A regular expression string to detect invalid strings is: 用于检测无效字符串的正则表达式字符串为:

[^\w \-\r\n]{2}|(?:[\w \-]+[^\w \-\r\n]){5}

As C# string literal (regular and verbatim): 作为C#字符串文字 (常规和逐字):

"[^\\w \\-\\r\\n]{2}|(?:[\\w \\-]+[^\\w \\-\\r\\n]){5}"

@"[^\w \-\r\n]{2}|(?:[\w \-]+[^\w \-\r\n]){5}"

It is much easier to find a string than to validate if a string does not contain ... 查找字符串比验证字符串是否不包含要容易得多。

It can be checked with this expression if the string entered by the user is invalid because of a match of 2 special characters in sequence OR 5 special characters used in the string. 可以使用此表达式检查用户输入的字符串是否无效,因为该字符串匹配2个特殊字符或该字符串中使用了5个特殊字符。

Explanation: 说明:

[^ ... ] ... a negative character class definition which matches any character NOT being one of the characters listed within the square brackets. [^ ... ] ...否定的字符类定义,它匹配任何不是方括号中列出的字符之一的字符。

\\w ... a word character which is either a letter, a digit or an underscore. \\w ...一个单词字符,可以是字母,数字或下划线。

The next character is simply a space character. 下一个字符只是一个空格字符。

\\- ... the hyphen character which must be escaped with a backslash within square brackets as otherwise the hyphen character would be interpreted as "FROM x TO z" (except when being the first or the last character within the square brackets). \\- -...必须在方括号内用反斜杠转义的连字符,否则连字符将被解释为“ FROM x TO z”(当是方括号内的第一个或最后一个字符时除外)。

\\r ... carriage return \\r ...回车

\\n ... line-feed \\n ...换行

Therefore [^\\w \\-\\r\\n] finds a character which is NOT a letter, NOT a digit, NOT an underscore, NOT a space, NOT a hyphen, NOT a carriage return and also NOT a line-feed. 因此[^\\w \\-\\r\\n]查找的字符不是字母,数字,下划线,空格,连字符,回车符和换行符。

{2} ... the preceding expression must match 2 such characters. {2} ...前面的表达式必须匹配2个这样的字符。

So with the expression [^\\w \\-\\r\\n]{2} it can be checked if the string contains 2 special characters in a sequence which makes the string invalid. 因此,使用表达式[^\\w \\-\\r\\n]{2}可以检查字符串中是否包含2个特殊字符,从而使该字符串无效。

| ... OR ... 要么

(?: ... ) ... none marking group needed here for applying the expression inside with the multiplier {5} at least 5 times. (?: ... ) ...此处不需要任何标记组,以至少将乘数{5}应用于表达式内部至少5次。

[ ... ] ... a positive character class definition which matches any character being one of the characters listed within the square brackets. [ ... ] ...一个肯定的字符类定义,它与方括号中列出的字符之一匹配的任何字符。

[\\w \\-]+ ... find a word character, or a space, or a hyphen 1 or more times. [\\w \\-]+ ...找到一个或多个单词字符,一个空格或一个连字符1次或更多次。

[^\\w \\-\\r\\n] ... and next character being NOT a word character, space, hyphen, carriage return or line-feed. [^\\w \\-\\r\\n] ...并且下一个字符不是单词字符,空格,连字符,回车符或换行符。

Therefore (?:[\\w \\-]+[^\\w \\-\\r\\n]){5} finds a string with 5 "special" characters between "standard" characters. 因此(?:[\\w \\-]+[^\\w \\-\\r\\n]){5}找到一个字符串,在“标准”字符之间包含5个“特殊”字符。

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

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