Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 581
Iain Ward Avatar asked Jun 09 '11 08:06

Iain Ward


People also ask

How do you match a capital letter in regex?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.

What is ?: In regex?

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

Can you use regex with numbers?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.


1 Answers

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

like image 144
codaddict Avatar answered Sep 23 '22 16:09

codaddict