Regular Expressions

The why and how…

Robert M Ricci
Mar 7 · 5 min read
Photo by Joshua Aragon on Unsplash

I had to use a regular expression or regex for short while working on a leetcode problem the other day and got to thinking about how little I know about them. So I decided to look into them and figure out what they are and why they came about. I also wanted to give examples of how to use them in your programs.

A regular expression is a search pattern that is specified by a sequence of characters. You would mostly use them in string search algorithms for find or find and replace. The concept first came into existence in the 1950s when the idea of a regular language was developed by Stephen Cole Keene. It came into common use for Unix test-processing utilities. It started to gain traction in the 1980s when different syntaxes were developed the most widely used one being the Perl syntax. A regular expression can make search and replace a breeze once you master them, and are useful even before that. I’m going to go over some examples. The images are from a ruby regex site called Rublar. I will link it below, as well as one I found for javascript.

SINGLE WORD SEARCH

The first example is going to be a search for just one word. Just to show you a basic search using regex. As you can see I’m just searching got the word “The”, with a capital “T”. That’s why it's only highlighting the “The”.

If I was to add an “i” flag to the end of the regex, it would ignore the case and highlight both “the”s.

Those examples are to show a basic word search. Next, I will show how to do character searches and some of the modifiers you can add to find what you are looking for.

CHARACTER SEARCH

For this section, we are going to search for just a specific character. We will add some modifiers to show how to narrow in on what you are searching for. In this example, I have removes the “i” flag, but instead, put both “e” and “E” into brackets so it would search for both lower and upper case.

In this example, we are searching for a grouping of letters that are two characters long. The square brackets mark the group, and the curly brackets mark the size of the group.

You could also just search for all characters. This one searches for just lower case.

You could search for both upper and lower as well.

For the last example, we are going to search for a phone number. This provides a challenge of formating. Phone numbers can be entered in so many ways, it has to make sure you are accounting for all of them. First, we will just search for a ten-digit number, we will use the “\d” which is the digit selector, and a number modifier “{10}”. You will see that it will only give us the first number because it isn't looking at the dashes.

We can fix this issue by asking the regex to look for dashes, as well as specifying how many digits are in a grouping. You will also notice the use of the “?”, this just means that the dash is optional.

Ok, so now we can search for a number without any other characters, and one with dashes. Now we need to search for a number with spaces. The syntax for this is going to be fairly similar, we will just need to add the parameter for spaces.

Now with a space added to the search parameters, all three versions of the phone can be searched.

SPECIAL SELECTORS

I just wanted to show some special selectors. The first one I wanted to show is the “\.” selector, which will just select everything.

You could also use the “\w” for any word character (letter, number, underscore).

Or the inverse of that you could use “\W” for any non-word. Digit is the same “\d” for digit like we used in the phone number example and “\D” for non-digit.

CONCLUSION

That’s an intro to regex. I would in courage you to dive into it. It’s really interesting and useful. I know I plan on trying to incorporate more of them into my programs.

Resources

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store