简体   繁体   English

如何编写仅在字符串包含某些字符和数字时才能激活的正则表达式?

[英]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: 我正在尝试解决一些我的老师给我的问题,其中一个问题是让我检查输入字符串是否符合以下规则:

  1. That the string contains numbers from 2 to 9. 该字符串包含2到9之间的数字。
  2. That the string contains the letters TJQKA. 该字符串包含字母TJQKA。
  3. If within the string there is any other letter outside the mentioned ones, then the String is invalid. 如果字符串中提到的字母之外还有其他字母,则该字符串无效。

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.

相关问题 我正在寻找一个正则表达式来允许 6 个字符,并且在 6 个字符内,它应该只允许 3 个字母和 3 个数字 - I am looking to write a regular expression to allow 6 characters and within the 6 characters, It should only allow 3 alphabets and 3 numbers 如果字符串只包含重复字符,那么查找正则表达式是什么? - What is a regular expression to find if string contains only repeating characters? 字符串的正则表达式包含不在特定集合中的字符 - Regular expression for a string contains characters not in a specific set 正则表达式:匹配字符串直到某些字符 - Regular Expression: Matching a string until certain characters 嗨,我如何检查在 java 中接受数字、字符和特殊字符的正则表达式 - Hi how can i check regular expression which accept numbers,characters and special character in java 使用 String.split() 如何根据不包括某个字符串的正则表达式拆分字符串 - Using String.split() How can I split a string based on a regular expression excluding a certain string 用于检测字符串的正则表达式仅包含。 要么 ? 要么? - Regular expression to detect string contains only . or ? or? 我需要写一个仅包含0到9之间的数字的字符串 - I need to write a string that contains only numbers from 0 to 9 验证文本的正则表达式,其中包含某些特殊字符 - Regular expression of validating text which contains certain special characters on condition 如何验证文本框中的字符串仅包含2个字符和3个数字? - How to validate that a string from a textbox contains only 2 characters and 3 numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM