2

Here's the question that was already posted: Regex to match and validate internet media type?

This works, for sure But fails in some of these:

*/plain
text/-vnd.MS.office-document.WORD+xml-
text/-vnd.MS.office_document.WORD+xml.
text/.vnd.MS.office_document.WORD+xml-
text/.vnd.MS.office-document.WORD+xml.
text/-api-format
text/.api.format
text/.api-format
text/-api.format
text/api-format-
text/api.format.+json
text/api-for_mat.
text/api.format-+json
text/api.-for_mat
text/api-.format+json

The Regex must not match the above mentioned string.

But must match the following string

*/*
text/*
image/jpg
image/x.apng+png
image/x-jpg2000+jpg
text/api.format
text/api-format+json
text/prs.NIRIN.config+xml
text/vnd.MS.office-document.WORD+xml

I'm a novice in Regex but I came up with this:

^(?<Base>application|audio|example|image|message|model|multipart|text|video)/(?<Extension>prs\.|vnd\.|x\.|x-)?(?<Sub>[\w-[_]]+(?:[.-]?[\w-[_]]+)*)+(?:\+(?<Super>[\w-[_]]+))?$

But it matches only a subset and matches a non-match too!!

But I want it like this:

Base: *, <any of the above 'Base'>
Extension: prs. , vnd. , x. , x- <legacy types, optional>
Sub: MS.office-document.WORD <if it contains nested types like this>
    Capture 1: MS
    Capture 2: office-document
    Capture 2: WORD
Super: xml <optional, rules same as 'Base' types and captures of 'Sub'>

Am I doing something wrong here?

Will it be too complex?

Can we simplify it further even with the proposed rules?

Note: this is .net flavored regex. so we can use .net specific features too!

Thanks.

Nirmal4G
  • 23
  • 1
  • 4

0 Answers0