[英]Regex, string is a combination of whitelisted characters and doesn't contain any in blacklist
1) How to regex validate user input to contain any combination of characters from group A and to not contain any characters from another group D? 1)如何正则表达式验证用户输入是否包含A组中的任何字符组合,以及不包含来自另一组D的任何字符?
2) Also check string length is between 2 and 255 2)同时检查字符串长度是否在2到255之间
In other words, for all string characters: A AND NOT D. 换句话说,对于所有字符串字符:A AND NOT D.
I receive two groups of characters (whitelist and blacklist) from a server and need to validate user input based on those. 我从服务器收到两组字符(白名单和黑名单),需要根据这些字符验证用户输入。 I cannot affect the design and must live with it.
我不能影响设计,必须忍受它。 I also must use regex because of other design restrictions.
由于其他设计限制,我也必须使用正则表达式。
Here's what I've got so far (not working at all): 这是我到目前为止所做的事情(根本不工作):
/^(?![23]+)[0-9]{2,255}$/
23 would be the blacklist of characters for simplicity 为简单起见, 23将是黑名单
0-9 would be the whitelist of characters for simplicity 为简单起见, 0-9将是字符的白名单
Some examples: 一些例子:
3014567890 --> fail, 3 is present 3014567890 - >失败,3存在
0145678902 --> fail, 2 is present 0145678902 - >失败,2存在
0123456789 --> fail, 2 and 3 are present 0123456789 - >失败,2和3存在
014567890 --> ok 014567890 - >好的
88774411489 --> ok 88774411489 - >好的
5 --> fail, not enough characters 5 - >失败,没有足够的人物
1abc --> fail, abc illegal chars 1abc - >失败,abc非法chars
ab1c --> fail, abc illegal chars ab1c - >失败,abc非法的chars
abc1 --> fail, abc illegal chars abc1 - >失败,abc非法的字符
Thanks! 谢谢!
You're nearly there, the lookahead assertion needs some work: 你几乎就在那里,先行断言需要一些工作:
/^(?!.*[23])[0-9]{2,255}$/
That way, the regex within the negative lookahead matches if there is (at least) one 2
or 3
anywhere in the string (ie, after any number of characters ( .*
)), causing the assertion to fail. 这样,负向前瞻中的正则表达式匹配,如果字符串中有(至少)一个
2
或3
(即,在任意数量的字符( .*
)之后),导致断言失败。
In this (apparently) simplified example, you could of course just have used /^[014-9]{2,255}$/
. 在这个(显然)简化的例子中,你当然可以使用
/^[014-9]{2,255}$/
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.