Search code examples
javaregexstringmatching

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

Solution

  • You may use this regex:

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

    RegEx Demo