In the three weeks I've completed so far of the Web Development Immersive course at the Flatiron School, I've learned a lot. I've worked with the command line, Git, Ruby, and SQL — just a few of the many languages, systems, and tools developers use to accomplish their work. Among all of these, one stands out as singularly useful and powerful: regular expressions.

Regular expressions are a sequence of characters that denote a pattern to be searched for within a block of text. When you need to find some text that matches some known criteria, but don't know what the actual words themselves are, regular expressions are a great tool to use.

For example, let's say you needed to find every instance of a person's first and last name within the following block of text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque varius risus quis nibh placerat, nec interdum quam varius. Nam ut laoreet turpis, sit amet pulvinar elit. Quisque posuere rutrum tortor, at maximus elit consequat quis. Duis non nisl in arcu commodo tempus. Sed vehicula varius dolor sed venenatis. Sed id urna eget massa Steve Jobs sodales efficitur. Suspendisse nisi lacus, varius id fringilla a, commodo ut libero. Nulla vestibulum aliquam eros eget vestibulum. Nullam et lobortis dui. Cras tempor lorem tempor massa tincidunt pharetra. Nunc viverra tempor eros non interdum. Aliquam erat volutpat. Vestibulum eu magna blandit, facilisis Bill Gates purus at, viverra erat. Donec viverra pulvinar arcu. Ut dolor augue, porttitor eu lacinia nec, aliquet sed leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Vestibulum auctor molestie neque, maximus lacinia mauris euismod cursus. Duis quis turpis fringilla, scelerisque leo ac, tincidunt lacus. Vestibulum semper fermentum est, quis ultrices metus fringilla vel. Morbi laoreet pretium luctus. Pellentesque turpis sem, dignissim bibendum Jeff Bezos elit sit amet, lobortis vehicula nunc. Cras quis nisl a nulla mattis pellentesque sit amet nec urna. Suspendisse in magna non erat vestibulum feugiat. Quisque tristique dui sit amet dui vehicula, sit amet iaculis lacus tempor.

Not so easy, is it?

Now, imagine that the text you have to search through is long enough to fill an entire book. You're a busy person, so you don't have the time to read through an entire book just to find every instance of a person's name. Besides, even if you were to do that, you might miss more than a few names along the way. Luckily, regular expressions make it simple to find just what we're looking for — even when it's buried in mounds of text.

To find a person's first and last name, we can use the following regular expression:

[A-Z]\w+\s[A-Z]\w+

In English, this translates to "a capital letter followed by any number of word characters, a space, a capital letter, and any number of word characters."

Try pasting the text and regular expression above into Rubular, and what do we get?

Steve Jobs, Bill Gates, Jeff Bezos

Bingo!

This example is pretty simple, but regular expressions are useful in a wide variety of situations — one common application is to check that the text entered in the "e-mail address" field on a form is, in fact, a valid e-mail address. Another useful application is for validating dates.

The most useful sites I've come across for learning about and testing regular expressions are RegExr and Rubular — give them a try, and the next time you need to find a needle in a haystack, you can use your newfound skills to save the day!

Posted
AuthorIan Valentine