Regexp format "YYYY/NNNN" or "YYYY/NNNN/Country code"

Viewed 121

I have this regexp: (\d{4})\/(\d{2,4})\/?(\w{2})? which seems to work properly, but I want to avoid the cases in which for example: I have a string "2020/2000/WEEEE" and it matches it.

Other wrong example that it matches: "IEN/BSK-2020/143999" .

How can I avoid this and take only the strings with the specified format from above? "YYYY/NNNN" and "YYYY/NNNN/Country code"

1 Answers

You can match the first part with the digits and the forward slash, and make the whole last part including the / optional asserting a whitspace boundary at the right using (?!\S)

\b(\d{4})\/(\d{2,4})(?:\/(\w{2}))?(?!\S)

Regex demo

A bit more precise pattern, where the country code can only be chars a-z, and without escaping the forward slash (when it is not the delimiter). If you don't need the 3 capture groups as separate values for processing, you can omit them.

\b(\d{4})/(\d{2,4})(?:/([a-zA-Z]{2}))?(?!\S)

Regex demo

Related