Regex pattern for capital letters and numbers only, with possible 'List'

只谈情不闲聊 提交于 2020-01-11 19:59:24

问题


What is the regex to match words that have the pattern:

Number or Capital in any order * 3 (+possible 'List' on the end)

For example,

OP3
G6H
ZZAList
349
127List

are all valid, whereas

a3G
P-0List
HYiList
def
YHr

are all invalid.


回答1:


You can use the regex:

^[A-Z0-9]{3}(?:List)?$

Explanation:

^        : Start anchor
[A-Z0-9] : Char class to match any one of the uppercase letter or digit
{3}      : Quantifier for previous sub-regex 
(?:List) : A literal 'List' enclosed in non-capturing paranthesis
?        : To make the 'List' optional
$        : End anchor

See it



来源:https://stackoverflow.com/questions/6290173/regex-pattern-for-capital-letters-and-numbers-only-with-possible-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!