[英]How can I write a regular expression that is only activated if the string contains certain characters and numbers?
I'm trying to develop some problems that my instructor gave me, and one of those problems is asking me to check if the input string complies with the following rules: 我正在尝试解决一些我的老师给我的问题,其中一个问题是让我检查输入字符串是否符合以下规则:
EX: 例如:
643TJAKQ = Ok 643TJAKQ =好
72MAJTQ = Not Okay 72MAJTQ =不好
1246AKJQ = Not Okay 1246AKJQ =不好
AKT432 = Ok AKT432 =好
This obviously is a Regex problem, because if I try to filter by lists, arrays or something else, It's going to take too much time just validating this (And I writting the code). 显然这是一个正则表达式问题,因为如果我尝试按列表,数组或其他内容进行过滤,那么仅通过验证(并编写代码)就会花费太多时间。 I tried with this reg exp (
/[2-9]|(T|J|Q|K|A)/g
) but is garbage. 我尝试使用此reg exp(
/[2-9]|(T|J|Q|K|A)/g
)但很垃圾。
the following regex works: ^[2-9TJQKA]+$
以下正则表达式有效:
^[2-9TJQKA]+$
explanation: 说明:
^
: start ^
:开始
[2-9TJQKA]
: define allowed values [2-9TJQKA]
:定义允许的值
+
: allow more than one character Strings +
:允许多个字符的字符串
$
: end $
:结束
您可以简单地定义允许的字符:
[2-9TJQKA]+
Boolean okayOrNot = someString.matches("[2-9TJQKA]+");
I would recommend the regex: \\b[2-9TJQKA]+\\b
我建议使用正则表达式:
\\b[2-9TJQKA]+\\b
2-9
-accept only numbers between 2 and 9 2-9
2和9之间-accept只有数字
TJQKA
-accept only these characters TJQKA
-Accept只有这些字符
\\b
-only words with only these characters \\b
仅包含这些字符的单词
regex101 also on free formatter regex101也在免费格式化程序上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.