I want regex to match either a-z or 0-9 characters of any length any order or exact string N/A.
Matching and nonmatching examples are below:
-
N/A
– match -
asd6fg
– match -
123asd
– match -
asd234
– match -
12sd23
– match -
as23fd
– match -
NA
– no match -
N/AN/A
– no match -
N/
– no match -
N
– no match -
123N/A
– no match -
abcN/A
– no match -
a1bN/A
– no match -
N/Aab2
– no match -
AS123
– no match
2
Answers
This should do it:
^
inidicates the start of input ,(?:...)
creates a non-capturing group; then either[all lowercase letters and digits]+
or|
N/A
are matched.$
marks the end of inputJust use this:
Explanation:
^
and$
say indicate the start and end of the string, respectively. By putting these at the ends of the RegEx, you’re saying that it should only match if it can match everything. It’s all or nothing.[a-z0-9]+
will match a-z and 0-9 one or more times.|
is the logical OR. It will either match the thing before it ([a-z0-9]+
) or the thing after it, which is…N/A
which is the exact string "N/A". You need to escape that/
character.Example: