dimanche 27 avril 2014

Capturer des arbres MIME possibles avec Regex - Stack Overflow


I'm trying to make a capture group to grab the tree portion of a MIME type. i already know that the input text is a MIME type. As examples:


MIME Type Matched portion


text/plain                           -> No match
application/x-deb -> "x"
application/vnd.google-earth.kml+xml -> "vnd"
application/x.example123 -> "x"
application/prs.vanity -> "prs"
application/prs-vanity -> No match

Currently, I have /(?:(prs|vnd|x)\.)|(x-)?, but I'm trying to consolidate it into a single capture group. Is this possible?


The only other try I've done is /(?:(prs|vnd|x)[\.-])?, but that matches, for example, application/vnd-badly-made as vnd instead of properly not matching anything.




One possible approach: match something that's preceded by application/ and is...



  • either x symbol followed by -,

  • or a group of letters followed by .


... with this pattern:


/(?<=application\/)(?:x(?=-)|[a-z]+(?=\.))/

Demo. If lookbehind is not an option, use capturing group:


/application\/(x(?=-)|[a-z]+(?=\.))/



What about this:


application/(?:(prs(?!-)|vnd(?!-)|x))(?=\.|-)

It matches all your cases.



I'm trying to make a capture group to grab the tree portion of a MIME type. i already know that the input text is a MIME type. As examples:


MIME Type Matched portion


text/plain                           -> No match
application/x-deb -> "x"
application/vnd.google-earth.kml+xml -> "vnd"
application/x.example123 -> "x"
application/prs.vanity -> "prs"
application/prs-vanity -> No match

Currently, I have /(?:(prs|vnd|x)\.)|(x-)?, but I'm trying to consolidate it into a single capture group. Is this possible?


The only other try I've done is /(?:(prs|vnd|x)[\.-])?, but that matches, for example, application/vnd-badly-made as vnd instead of properly not matching anything.



One possible approach: match something that's preceded by application/ and is...



  • either x symbol followed by -,

  • or a group of letters followed by .


... with this pattern:


/(?<=application\/)(?:x(?=-)|[a-z]+(?=\.))/

Demo. If lookbehind is not an option, use capturing group:


/application\/(x(?=-)|[a-z]+(?=\.))/


What about this:


application/(?:(prs(?!-)|vnd(?!-)|x))(?=\.|-)

It matches all your cases.


Related Posts:

0 commentaires:

Enregistrer un commentaire