Regular Expressions for Beginners

Nimesha Wijepala
3 min readNov 19, 2020

Introduction

A Regular expression alias a Regex is simply a sequence of characters that we can use to identify a pattern. Learning how to write a regular expression is much worth in software development because it can be used for both day-to-day validations as well as identifying search patterns in coding. Well, let’s learn the concepts.

Basic Character Symbols & Patterns

First of all, we have to know what kind of characters we have to use for identifying patterns. The below table describes mostly used symbols in regular expressions.

Meta Characters

Other than character symbols and patterns we discussed earlier there are some meta-characters that can be used as short-codes, simply all in one. Let’s see how.

Quantifiers

Now let’s see what are the characters which we can use for searching the number of occurrences of a regular expression.

We can add the quantifier at the end of the expression. See the example below.

A?B+

The above expression means that A should occur zero or one time and B should be one or more times. Some of the strings that match with the above expression have been displayed below.

B, AB, ABB, BBB

Regex Implementation for Day to Day Validations

Let’s enter the game now. Every programmer has to validate user inputs no matter what the programming language is. We can use several correct regular expressions for one scenario. But we have to know how to build at least one. Let’s see how to write regex for some important validation scenarios in programming.

Mobile Number Validation

The following describes a search pattern for a mobile number of 10 digits. A mobile number should contain only digits, sometimes a hyphen, and sometimes a “+” sign with a country code. Let’s discuss how to construct the below expression for a 10 digit mobile number pattern step by step.

^(+\d{4}|\d{3}|\d{6})(-)?(\d{7})$

Email Validation

When we have to implement validations for forms or login pages it is really important knowing how to write a regular expression for email validation. First, we have to identify what an email address contains in what places. Second, it is easy to build the regular expression as groups step by step. Actually, email address patterns can be different as per the requirement. Anyway, let’s discuss the below for one example pattern.

^([a-zA-Z0–9]+[!#$%&’*+-/=?^_`{|}~]?[a-zA-Z0–9]?)@([a-zA-Z0-9-]+)(.[a-zA-Z0–9-]+)+$

Password Validation

Password validation is a bit different than the two scenarios we discussed before. In previous scenarios, we discussed how to validate expressions with a particular sequence. But the password is not like that. It should contain specific characters but not in a specific place in the string. Here is a regular expression for a password with the conditions below.

It should contain at least one digit.

It should contain at least one lowercase letter.

It should contain at least one uppercase letter.

It should contain at least one special character.

Its length should be 8 characters or more.

Let’s discuss how to construct the regex with the mentioned conditions.

^(?=.*[0–9])(?=.*[a-z])(?=.*[A-Z]+)(?=.*[!@#&()-_[{}]:;’,?/*~$^+=<>]).{8,}$

Implementation in java

No matter what the programming language is, we can implement regular expressions for our validations. Following is one example of java code for matching a regular expression with the input string.

The regular expression used in the code is the one we discussed earlier, for email validation. In java, java.util.regex.* is the package that has been written for implementations related to the regular expressions. Here we have used the compile method of Pattern class and the matches method of Matcher class. There are also some other methods in the package which we can use for regex implementation as per the requirement.

Conclusion

Well, memorizing all concepts inside regular expressions is not quite easy. But knowing how to construct a regular expression is very important to a programmer to make coding an easy task for validation and searching scenarios. Also, it really increases the thinking capacity. So let’s try out today onwards.

--

--

Nimesha Wijepala

One who work in software industry as well as loves writing. Trying to combine both for sharing knowledge all over the world.