Skip to content

Instantly share code, notes, and snippets.

@maxgfr
Created September 28, 2020 13:13
Show Gist options
  • Save maxgfr/85c45695128896fc5438a3ba056479a9 to your computer and use it in GitHub Desktop.
Save maxgfr/85c45695128896fc5438a3ba056479a9 to your computer and use it in GitHub Desktop.
List of useful regex

Useful Regex

From this repository : https://github.com/OpenCode/awesome-regex

Table of Content

Information technology

Hex color

/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
String Match
#C0FF33
#000000
#fff
#ffffff
#FFFFFF

IP Address

/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/
String Match
192.168.1.1
255.255.255.0
8.8.8.8

MAC Address

/^(?:[0-9a-f]{2}[\-:]?){6}(?<![\-:])$/i
String Match
AA:BB:CC:DD:EE:FF
AA-BB-CC-DD-EE-FF
AA:BB-CC:DD-EE:FF
AABBCCDDEEFF
AA.BB.CC.DD.EE.FF
AA-BB-CC-DD-EE-GG
AA:BB:CC:DD:EE:FF:
AA:BB:CC:DD:EE:FF-

Mail

/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
String Match
[email protected]
[email protected]
foo@bar

URL

/^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$/

Spotify track

/^https?:\/\/(?:open|play)\.spotify\.com\/track\/[\w\d]+$/i

Spotify playlist

/^https?:\/\/(?:open|play)\.spotify\.com\/user\/([\w\d]+)\/playlist\/[\w\d]+$/i

Soundcloud

/^https?:\/\/soundcloud\.com\/\S+\/\S+$/i
String Match
https://soundcloud.com/geierre/only-tonight

Deezer track/album/playlist

/^https?:\/\/(?:www\.)?deezer\.com\/(track|album|playlist)\/(\d+)$/

Youtube

/^https?:\/\/(?:(?:www|m)\.)?(?:youtube\.com\/watch(?:\?v=|\?.+?&v=)|youtu\.be\/)([a-z0-9_-]+)$/i
String Match
http://www.youtube.com/watch?v=dbB-mICjkQM
https://www.youtube.com/watch?v=dbB-mICjkQM
https://m.youtube.com/watch?v=dbB-mICjkQM
https://youtu.be/dbB-mICjkQM

Vimeo

/^https?:\/\/(?:www\.)?vimeo\.com.+?(\d+).*$/i
String Match
https://vimeo.com/58165438
http://vimeo.com/58165438

Dailymotion

/^https?:\/\/(?:www\.)?(?:dai\.ly\/|dailymotion\.com\/(?:.+?video=|(?:video|hub)\/))([a-z0-9]+)$/i
String Match
http://www.dailymotion.com/video/x3pu5i0

Facebook photo / video

/^https?:\/\/(?:www\.)?facebook\.com\/(?:(?:photo|video)\.php(?:\?v=|\?.+?&v=)|[a-z0-9._-]+\/videos\/)(\d+)\/?$/i

NERDZCrush

/^https?:\/\/(?:cdn\.)?media\.nerdz\.eu\/([a-z0-9_-]{12})(?:|\.[a-z0-9]{2,4})$/i

Imgur

/^https?:\/\/(?:www\.)?(?:i\.)?imgur\.com\/([a-z0-9_-]+)\.(?:gifv|webm)$/i
String Match
http://i.imgur.com/w59wsnu.gifv

GitHub profile / organization

/^https?:\/\/(www\.)?github\.com\/[a-z0-9]+$/i
String Match
https://github.com/OpenCode

Twitter status

/^https?:\/\/twitter.com\/[a-z0-9_]+\/status\/[0-9]+$/i
String Match
https://twitter.com/_opencode/status/690479102978035712

Text manipulation

Remove CDATA tags

/<!\[CDATA\[(.*)\]\]>/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment