有人可以帮我写RegEx表达式吗,它可以检查一个字符串是否包含一个以上的大写字母(或小写字母,无关紧要)字母,但不能连续出现.我需要在一个字符串中至少出现两次(甚至更好的n次).如果n = 2,则有效情况为密码,密码或密码.
Can somebody please help me write RegEx expression which can check if a string contains more than one occurrence of a uppercase (or lowercase, doesn't matter) letter but not in a row. I need to have at least two (or even better n) occurrences in a string. If n=2 valid situations would be PassWord or PAssword or PASSWord.
当我尝试使用此/(?=([A-Z] {2,3}))/g 时,它匹配PassWOrd,但不匹配PassWord.
When I tried this /(?=([A-Z]{2,3}))/g it matched PassWOrd but not PassWord.
对我来说奇怪的是,它也与PaSSWOrd相匹配.我以为{2,3}中的3实际上意味着最多将匹配3个大写字符.SSWO为什么匹配?
What is strange to me is that it also matched PaSSWOrd. I thought 3 in {2,3} actually means that no more that 3 Uppercase characters will be matched. Why is SSWO matched?
我尝试了类似的变体,但没有一个对我有用(因为我对RegEx不太熟悉,这并不奇怪).
I tried similar variations but non of them worked for me (nothing strange as i'm not very familiar with RegEx).
可以使用RegEx做到吗?
Can this be done using RegEx?
推荐答案(?=([AZ] {2,3}))正则表达式匹配2到3个连续大写ASCII字母字符串中的任意位置.您想匹配一个仅包含2到3个大写ASCII字母的字符串,而不必连续.
The (?=([A-Z]{2,3})) regex matches 2 to 3 consecutive uppercase ASCII letters anywhere inside a string. You want to match a string that only contains 2 to 3 uppercase ASCII letters, not necessarily consecutively.
要匹配仅包含两个大写ASCII字母(不多不少)的字符串,请使用以下表达式:
To match a string that only contains two uppercase ASCII letters (no more no less), use the following expression:
^(?:[^A-Z]*[A-Z]){2}[^A-Z]*$或者,如果您只允许在整个字符串中使用ASCII字母:
Or, if you only allow ASCII letters in the whole string:
^(?:[a-z]*[A-Z]){2}[a-z]*$请参见 regex演示.
模式详细信息
- ^ -字符串的开头
- (?:[^ A-Z] * [A-Z]){2} -正好发生2次
- [^ A-Z] * -零个或多个字符(ASCII大写字母除外)
- [A-Z] -一个ASCII大写字母
- ^ - start of string
- (?:[^A-Z]*[A-Z]){2} - exactly 2 consequent occurrences of
- [^A-Z]* - zero or more chars other than ASCII uppercase letters
- [A-Z] - one ASCII uppercase letter
在C#中,使用
var strs = new List<string> { "PassWord", "PAssword", "PASSWord"}; var n = 2; var pat = $@"^(?:[^A-Z]*[A-Z]){{{n}}}[^A-Z]*$"; foreach (var s in strs) { Console.WriteLine("{0}: {1}", s, Regex.IsMatch(s, pat)); }结果:
PassWord: True PAssword: True PASSWord: False请参见在线演示
请注意,如果您需要在字符串中要求2个大写ASCII字母(其中其他字符可以是任何字符),则不需要正则表达式,请使用LINQ:
Note that in case you need to require 2 uppercase ASCII letters in a string where other chars can be any chars, you do not need a regex, use LINQ:
var strs = new List<string> { "PassWord", "PAssword", "PASSWord"}; var n = 2; foreach (var s in strs) { var res = s.Count(c => (c >= 65 && c <= 90)); Console.WriteLine("{0}: {1}", s, res == 2); }请参见另一个演示. .Count(c =>(c> = 65&& c< = 90))部分将计算字符串中任意位置的大写ASCII字母,而 res == 2 将返回一个布尔结果,无论该数字是否等于2.可以轻松调整以进行数字范围检查.
See another demo. The .Count(c => (c >= 65 && c <= 90)) part will count the uppercase ASCII letters anywhere in the string, and res==2 will return a boolean result, whether the number is equal to 2 or not. It can be adjusted for a numeric range check easily.
如果需要Unicode兼容性,请将 .Count(c =>(c> = 65& c< = 90))替换为 .Where(Char.IsUpper).
If you need Unicode compatibility, replace .Count(c => (c >= 65 && c <= 90)) with .Where(Char.IsUpper).
更多推荐
查找字符串中字符的多个非连续出现
发布评论