catch32
catch32

Reputation: 18612

Regex match any of the words separated by slashes

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

Upvotes: 3

Views: 445

Answers (3)

bobble bubble
bobble bubble

Reputation: 18555

This idea makes the pattern a bit shorter:

^(?:(?:pdf|csv|xlsx?)/?\b)+$

Here is a demo at regex101

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.

Upvotes: 1

anubhava
anubhava

Reputation: 786146

You may use this regex:

^(?:pdf|csv|xlsx?)(?:/(?:pdf|csv|xlsx?))*$

RegEx Demo

Upvotes: 2

Ivo
Ivo

Reputation: 23357

How about this?

^(pdf|csv|xlsx)(\/(pdf|csv|xlsx))*$

Upvotes: 1

Related Questions