Victor Schubert’s personal page

Regular expressions library

Here I compile some of the regular expressions I made and think might be useful later on. I will update this page sometimes if I come up with others.

IPv4

This regular expression will match valid IPv4 addresses: it checks that bytes are in the [0-255] range and allows leading zeroes.

0*(?:2(?:5[0-5]|[0-4][0-9])|1?[0-9]{1,2})\.0*(?:2(?:5[0-5]|[0-4][0-9])|1?[0-9]{1,2})\.0*(?:2(?:5[0-5]|[0-4][0-9])|1?[0-9]{1,2})\.0*(?:2(?:5[0-5]|[0-4][0-9])|1?[0-9]{1,2})

IPv6

This expression will match valid IPv6 addresses, with support for empty group substitution and groups with fewer than four characters.

(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})(?:\.(?:2(?:5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})){3}|[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){7}|:(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:)|[0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,6}|:)|[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,5}|:)|[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){2}:(?:(?::[0-9A-Fa-f]{1,4}){1,4}|:)|[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){3}:(?:(?::[0-9A-Fa-f]{1,4}){1,3}|:)|[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){4}:(?:(?::[0-9A-Fa-f]{1,4}){1,2}|:)|[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4}){5}:(?::[0-9A-Fa-f]{1,4}|:)

S3 Object URI

This expression will match an AWS S3 object URI. The s3:// part is optional. The capturing groups will match, in order, the bucket name without s3:// or trailing slash, and the key without leading slash.

^(?:s3:\/\/)?((?![^\/]{1,61}\.\.[^\/]{1,61})[a-z.-]{3,63})(?:\/(.{0,1024}))?$

This variant makes the leading s3:// mandatory.

^s3:\/\/((?![^\/]{1,61}\.\.[^\/]{1,61})[a-z.-]{3,63})(?:\/(.{0,1024}))?$