Dive Deeper into the World of Regex: Passwords, Gmail Tricks, Code Comments, and IPv6 - Part 2

  • June 4, 2023
  • 3 min read

Welcome back, intrepid code voyagers! If you're here, you've already taken your first steps into the fascinating world of Regular Expressions (regex) in our Part 1 blog post here. Now, it's time to deepen your understanding and expand your regex repertoire.

Today we'll be delving into a wider range of practical examples. These will help you secure passwords, identify special features in Gmail addresses, detect comments in code, and even unravel the complexities of IPv6 addresses. Sounds exciting? Let's jump right in!

• Solidifying Your Site's Security: Regex for Password Validation

Security is paramount in today's digital world. One of the most common uses of regex is password validation. Let's start simple and then level up.

A Simple Start: Basic Password Validation

Imagine we want to enforce the following rules: each password must have at least one uppercase letter, one lowercase letter, one number, and be at least six characters long. A straightforward regex for this might be:


Here's the low-down:

  • ^ and $ mark the start and end of the string, respectively.
  • Each (?=.*[character-class]) is a positive lookahead, ensuring that the password contains at least one character of the specified class.
  • .{6,} finally checks that the password is at least six characters long.

Leveling Up: Advanced Password Validation

But what if we want to raise the bar and demand that passwords also include at least one special character and be at least eight characters long? We just need to expand our regex a bit:


The added (?=.*[@$!%*?&]) ensures at least one special character is present. .{8,} now checks for a minimum of eight characters.

• Detecting Gmail's + Trick with Regex

Ever used the neat trick where Gmail ignores anything after a plus (+) in the email username? Let's say we want to check if an email address uses this feature. Here's a handy regex:


The magic here happens in the (\+[a-zA-Z0-9._%+-]+)?, which checks for optional characters after a plus sign in the email.

• Unraveling Comments in CSS, HTML, and PHP with Regex

Comments are crucial in every programmer's life, aiding in making the code more human-readable. What if we want to detect these friendly annotations? Once again, regex comes to the rescue!

CSS & HTML Comments

To find comments in CSS (/* This is a comment */), we can use this regex:


HTML comments (<!-- This is a comment -->) require a different pattern. Here's the regex for them:


This regex matches a string starting with <!--, followed by any character ([\s\S]*?), and ending with -->. The [\s\S]*? allows for multiline comments and ensures we don't run into trouble with multiple comments on the same line.

Together, these expressions have got your back in parsing the helpful notations found throughout your CSS and HTML code.

PHP Comments

And in case you missed it, we also covered PHP comments in our previous section, with a regex capable of capturing both multiline (/* comment */) and single-line (// comment) forms:


• IPv6 Detection with Regex

IPv6 addresses can be challenging due to their various possible formats. Fear not! With this regex, you can detect a valid IPv6 address:


Each {0,4} ensures there are at most four hexadecimal digits, while the {1,7} allows for up to seven groups of these digits, separated by colons.


And that's it for Part 2 of our regex journey, folks! We've delved deeper into the magic of regex, explored how it can enhance security, organize your inbox, decode comments, and even make sense of the intricate IPv6 addresses.

Remember, regular expressions are powerful tools in your developer's toolkit. With them, you can wrangle even the most unruly strings of text. Keep practicing, keep experimenting, and before you know it, you'll be a regex wizard!

In case you missed our first thrilling exploration into regex, be sure to catch up here. As always, stay tuned for more adventures in coding!

Happy coding, friends!

RegexRegular ExpressionsPassword ValidationGmail TricksCode CommentsCSSHTMLPHPIPv6Pattern MatchingCybersecurityWeb DevelopmentCoding SkillsAdvanced RegexPractical Regex Applications