Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to parse docker tag?

Tags:

regex

'registry/rabbit',
'registry/rabbit:3',
'rabbit',
'rabbit:3'

Trying to come up with a regex that will match rabbit in the four cases above. Seems easy enough, but my regex-fu is failing me.

like image 792
Steve Prentice Avatar asked Sep 24 '16 01:09

Steve Prentice


1 Answers

The format is a little under-specified, but this seems to work:

^(?:(?=[^:\/]{1,253})(?!-)[a-zA-Z0-9-]{1,63}(?<!-)(?:\.(?!-)[a-zA-Z0-9-]{1,63}(?<!-))*(?::[0-9]{1,5})?/)?((?![._-])(?:[a-z0-9._-]*)(?<![._-])(?:/(?![._-])[a-z0-9._-]*(?<![._-]))*)(?::(?![.-])[a-zA-Z0-9_.-]{1,128})?$

From the docs:

An image name is made up of slash-separated name components, optionally prefixed by a registry hostname. The hostname must comply with standard DNS rules, but may not contain underscores. If a hostname is present, it may optionally be followed by a port number in the format :8080. If not present, the command uses Docker’s public registry located at registry-1.docker.io by default. Name components may contain lowercase characters, digits and separators. A separator is defined as a period, one or two underscores, or one or more dashes. A name component may not start or end with a separator.

A tag name may contain lowercase and uppercase characters, digits, underscores, periods and dashes. A tag name may not start with a period or a dash and may contain a maximum of 128 characters.

Tests are here.

like image 128
Joe Hildebrand Avatar answered Oct 11 '22 21:10

Joe Hildebrand