DEV Community

Cover image for Why you never hate Regex again?
Malik
Malik

Posted on

Why you never hate Regex again?

Created by Geon George and available on GitHub, "I hate Regex" is a tool that allows you to quickly get a cheat sheet of multiple frequently used regular expressions.

Among them, here is a non-exhaustive list.

Username

^[a-z0-9_-]{3,15}$
Enter fullscreen mode Exit fullscreen mode

Date

(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})
Enter fullscreen mode Exit fullscreen mode

URLs

/(?:http|https|ftp|mailto|file|data|irc):\/\/[A-Za-z0-9\-]{0,63}(\.[A-Za-z0-9\-]{0,63})+(:\d{1,4})?\/*(\/*[A-Za-z0-9\-._]+\/*)*(\?.*)?(#.*)?/
Enter fullscreen mode Exit fullscreen mode

24-hour time (HH:MM)

/(?:2[0-3]|[01]\d|\d):(?:[0-5]\d)/
Enter fullscreen mode Exit fullscreen mode

Password

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,}$
Enter fullscreen mode Exit fullscreen mode

There are many uses of Regex and in many different programming languages, you will have to deal with this type of expression at some point. However, the most important thing to remember is that there are infinite ways of doing this so there are multiple ways to get the best solution.

Moreover, if you are in need of inspiration, there is a whole library on regex101.com

Top comments (0)