Black Bytes
Share this post!

Mastering Ruby Regular Expressions

Ruby regular expressions (ruby regex for short) help you find specific patterns inside strings, with the intent of extracting that data for further processing. Two common use cases for regular expressions are validation and parsing.

For example, think about an email address, with a ruby regex you can define what a valid email address looks like. That will make your program able to differentiate a valid email address from an invalid one.

ruby regex

Ruby regular expressions are defined between two forward slashes, to differentiate them from other language syntax. The most simple expressions just match a word or even a single letter, for example:

This will return the index of the first occurrence of the word if it was found or nil otherwise. If we don’t care about the index we could just use the String#include? method.

Character Classes

A character class lets you define either a range or a list of characters to match. For example, [aeiou] matches any vowel.

Example: Does the string contain a vowel?

This will not take into account the amount of characters, we will see how to do that soon.

Ranges

We can use ranges to match multiple letters or numbers without having to type them all out. In other words, a range like [2-5] is the same as [2345].

Some useful ranges:

  • [0-9] matches any number from 0 to 9
  • [a-z] matches any letter from a to z (no caps)
  • [^a-z] negated range

Example: Does this string contain any numbers?

Remember: the return value when using =~ is either the string index or nil

There is a nice shorthand syntax for specifying character ranges:

  • \w is equivalent to [0-9a-zA-Z_]
  • \d is the same as [0-9]
  • \s matches white space (tabs, regular space, newline)

There is also the negative form of these:

  • \W anything that’s not in [0-9a-zA-Z_]
  • \D anything that’s not a number
  • \S anything that’s not a space

The dot character . matches everything but new lines. If you need to use a literal . then you will have to escape it.

Example: Escaping special characters

Modifiers

Up until now we have only been able to match a single character at a time. To match multiple characters we can use pattern modifiers.

Modifier Description
+ 1 or more
* 0 or more
? 0 or 1
{3,5} between 3 and 5

We can combine everything we learned so far to create more complex regular expressions.

Example: Does this look like an IP address?

Exact Matching

If you need exact matches you will need another type of modifier. Let’s see an example so you can see what I’m talking about:

If you want to match strictly at the start of a string and not just on every line (after a \n) you need to use \A and \Z instead of ^ and $.

Capture Groups

With capture groups, we can capture part of a match and reuse it later. To capture a match we enclose the part we want to capture from the regular expression in parenthesis.

Example: Parsing a log file

In this example, we are using .match instead of =~. This method returns a MatchData object if there is a match, nil otherwise. MatchData has many useful methods, check out the documentation!

You can access the captured data using the .captures method or treating the MatchData object like an array, the zero index will have the full match and consequent indexes will contain the matched groups.

We can also have non-capturing groups. They will let us group expressions together without a performance penalty. You may also find named groups useful for making complex expressions easier to read.

Syntax Description
(?:...) Non-capturing group
(?<foo>...) Named group

Example: Named Groups

Look ahead / Look behind

This is a more advanced technique that might not be available in all regex implementations. Ruby’s regular expression engine is able to do this, so let’s see how take advantage of that.

Look ahead lets us peek and see if there is a specific match before or after.

Name Description
(?=pat) Positive lookahead
(?<=pat) Positive lookbehind
(?!pat) Negative lookahead
(?<!pat) Negative lookbehind

Example: is there a number preceded by at least one letter?

Ruby’s Regex Class

Ruby regular expressions are instances of the Regex class. Most of the time you won’t be using this class directly, but it is good to know 🙂

Formatting Long Regular Expressions

Complex Ruby regular expressions can get pretty hard to read, so it would be helpful if we broke them into multiple lines. We can accomplish this by using the ‘x’ modifier. This format also allows us to use comments.

Example:

Ruby regex: Putting It All Together

Regular expressions can be used with many Ruby methods.

  • .split
  • .scan
  • .gsub
  • and many more…

Example: Get all words from a string using .scan

Example: Capitalize all words in a string

Conclusion

Ruby regular expressions are amazing but sometimes they can be a bit tricky. Using a tool like rubular.com can help you build your ruby regex in a more interactive way. Rubular also includes a ruby regular expression cheatsheet that you will find very useful. Now it’s your turn to crack open that editor and start coding!

Oh, and don’t forget to share this with your friends if you enjoyed it, so more people can learn 😉

4 comments
Bernardo says last year

Hi, nice post! It’s worth pointing out, I think, that you can also get named capture groups with the =~ notation:

As far as I know it only works this way around, and not this way:

echristopherson says last year

Bernardo, some characters got stripped out in your comment, and the quotes got changed to curly quotes. It should be

    Jesus Castello says last year

    Hey, thanks for you comment. I edited it so it should look right now 🙂

jaki says last year

It’s always better to make your regular expression more specific e.g. searching for a word at the start of line and searching for a word in the line can make big performance difference, if majority of traffic is for unmatched input.

Comments are closed