Regular Expressions 101

Community Patterns

123...6

Mime type parser

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 - a year ago (Last modified a year ago)