Regular Expressions 101

Community Patterns

Your search did not match anything

Community Library Entry

2

Regular Expression
.NET 7.0 (C#)

@"
(?<type>\w+)\/(?<subtype>[\w\.-]+)(?:\+(?<suffix>[\w\.-]+))*(?:\s*;\s*(?<key>[^=]+?)(?:=""?(?<value>[\S\.-]+?)""?)?)*$
"
g

Description

A "simple" regex to parse mime types. Handles all commonly occurring parts of a mime type string such as:

  • Type
  • Subtype
  • Subtype's with suffix(s) (e.g. svg+xml)
  • Multiple parameters

Can also handle parameter values wrapped in double quotes and parameter keys without a value (so long as there is not a dangling '=' sign)

Tested with:

  • application/text
  • image/svg+xml
  • image/svg;q=0.9,/;q=0.8
  • image/svg+xml;q=0.9,/;q=0.8
  • application/vnd.uplanet.listcmd-wbxml;charset=utf-8

And many more in the Unit Tests tab

Submitted by Vespion - 2 years ago (Last modified a month ago)