DEV Community

Cover image for Creating patterns with regex

Posted on • Updated on

Creating patterns with regex

I'm in the early stages of my journey to become a software engineer and I am delighted to be here. In life and now in code, I really enjoy looking at and analyzing patterns. So when I came across this thing I'm about to tell you all about, I just knew that I had to learn more about it!

During my studies, I have had the pleasure of working through multitudes of practice problems (I’m sure you can relate!). Many problems want you to write code to find a certain character within a string or that has the ability to split a string in a particular way.

No doubt- it’s important to learn how to do this using that particular language so that you can fully understand and utilize the language itself, but depending on the problem you're trying to solve, the process can be long or complicated. You should know that there is a way to save you lots of time.

I’m talking about regular expressions, or regex, which allow you to define a search pattern using a particular sequence of characters. This technique is commonly used on strings for parsing operations like “find” or “find and replace” and for input validation (like verifying an email address).

Some common languages that you can use regex with are:

  • JavaScript
  • PHP
  • Python
  • Ruby
  • Swift

And more…

A tiny bite of history: regex is a concept that was developed in the 1950s when code had to be as terse as possible in order to conserve memory which was very limited. As a result, it can be quite cryptic and complex to decipher. Note that regex is not actually a language itself, it is really a set of standard syntax that is used when parsing strings or text.

Yes, regex can be complicated, and to fully understand it is beyond the scope of this post and of the writer at this time. That said, by learning some regex basics, you can enhance your programming in very powerful ways.

Though there are many uses for regex, we’ll go over just a few of the commonly used features here. And since I’m currently learning Ruby, all examples here are in Ruby.

Let’s look at some examples:

String literals can assist you in finding a specific piece of text. The regex operator "=~" parses the string and returns the first index at which a match is found. If no match is found it will return nil.

text = "We love cats!"

text =~ /cats/
#=> returns the index 8

text =~ /dogs/ 
#=> returns nil

Say you were given a string of sentences containing mixed punctuation and you wanted to return an array where each element is an individual sentence as a string. If the sentences all ended with the same type of punctuation, you could simply call the split method on the string, passing in the punctuation type as an argument. But this string has several types of punctuation, so what do we do? Well regex makes it easy!

Alt Text

This will give you:

Alt Text

Please note: we could also pass the regex pattern directly into the split method as an argument instead of storing it inside a variable.
Storing it inside the variable pattern allows us to use it again and again!

So what is going on with that pattern? Well, inside the two forward slashes is our regex syntax where we are telling Ruby to take action using the split method whenever we encounter ".", "?", OR "!". Isn't that super?

Say you wanted to count the number of sentences within some text- but this text also contains multiple types of punctuation. Regex makes that easy too!

Alt Text

In this example we are following a similar structure as the previous example except now we are adding a quantifier. This will specify how many times we expect a certain character to occur, so the "+" at the end of the pattern simply indicates that we want to allow for multiple matches.
Our expected return in this example is 11.

We have all encountered a problem that asks us to parse a string to return all the vowels within the string. Regex will make this easy for us by using the scan method with a character class. Character classes are used to define a set of desired characters. It looks like this:

Alt Text

And returns this:

Alt Text

BUT WAIT! You must be wondering why the capital "A" did not show up in the return. This is because regex is case-sensitive, so try:

Alt Text

And now this returns all vowels from the string regardless of case.

Alt Text

Here's a short guide to some commonly used expressions in Ruby:

[xyz]    single character: x, y, or z
[^xyz]   single character except: x, y, or z
[a-z]    single character in range of a-z
[a-zA-Z] single character in range of a-z or A-Z
(y|z)    y or z

 \d  any digit
 \D  any non-digit
 \w  any word character (letter, number, or underscore)
 \W  any non-word character

 x?  zero or one of x
 x*  zero or more of x
 x+  one or more of x

 x{4}    exactly 4 of x
 x{4,}   4 or more of x
 x{4,6}  between 4 and 6 of x

A quick search will reveal the many ways that you can use regex to simplify your code and your life. Don't let it's cryptic nature deter you!

When in doubt just consult a regular expression editor, but make sure you are using one that matches the language that you are writing in. Remember that regex is a valuable tool that can be used across many common languages. Have fun with it and thanks for reading!

Top comments (0)