我需要一个正则表达式,它将验证字符串的长度为7,不包含元音,数字0和数字1.
我知道类似于角色类,[a-z] 但是必须以这种方式指定每种可能性似乎很痛苦:[2-9~!@#$%^&*()b-df-hj-np-t...]
例如:
如果我传递一个字符串June2013- 它应该失败,因为字符串的长度是8,它包含2个元音,数字0和1.
如果我传递一个字符串XYZ2003- 它应该失败,因为它包含0.
如果我传递一个字符串XYZ2223- 它应该通过.
提前致谢!
So that would be something like this:
^[^aeiouAEIOU01]{7}$
Run Code Online (Sandbox Code Playgroud)
The ^$ anchors ensure there's nothing in there but what you specify, the character class [^...] means any character except those listed and the {7} means exactly seven of them.
That's following the English definition of vowel, other cultures may have a different idea as to what constitutes voweliness.
Based on your test data, the results are:
pax> echo 'June2013' | egrep '^[^aeiouAEIOU01]{7}$'
pax> echo 'XYZ2003' | egrep '^[^aeiouAEIOU01]{7}$'
pax> echo 'XYZ2223' | egrep '^[^aeiouAEIOU01]{7}$'
XYZ2223
Run Code Online (Sandbox Code Playgroud)