I need to develop a validation pattern for catching any kind from:
pdf/csv/xlsx
select any separate word is simple:
(pdf|csv|xlsx).
However, I need to check that any combination with them separated by slash is fine as well.
For example, correct flow:
pdf/csv/xlsx
pdf
pdf/csv
csv/xlsx
xlsx
pdf/xlsx
Incorrect:
test
incorrect
doc
CodePudding user response:
You may use this regex:
^(?:pdf|csv|xlsx?)(?:/(?:pdf|csv|xlsx?))*$
CodePudding user response:
How about this?
^(pdf|csv|xlsx)(\/(pdf|csv|xlsx))*$
CodePudding user response:
This idea makes the pattern a bit shorter:
^(?:(?:pdf|csv|xlsx?)/?\b) $
The slash is set to optional here and the word boundary \b
requires it between words on the one hand and disallows it at the end on the other. It might be a tiny bit less efficient, but looks cool.