Regular Expressions 101

Community Patterns

Community Library Entry

2

Regular Expression
.NET 7.0 (C#)

@"
(?<type>\w*)\/(?<subtype>[\w\.-]*)(?:\+(?<subtype>[\w\.-]*))?(?:;(?:(?<key>.+)=(?<value>.*);)*(?<key>.+)=(?<value>.*))?
"
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 an extra extension (e.g. svg+xml) (Optional input)
  • Multiple parameters (Optional input)

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
Submitted by Vespion - 2 years ago (Last modified 2 years ago)