Regex 101

By April 10, 2019 Uncategorized

A well thought out Regular Expression, or regex can be elegant and mysterious. In Ruby, there are a number of ways to elegantly use a regex to accomplish tasks that might take several lines of code. Let’s start off with some basic examples and work up to some more complex ones.

Feel free to try out either of the following examples on the regex tool rubular.

One of the most simple and common ways of using a regex is to search a string for a pattern of characters. We can do this using the match method.

Let’s say we have several phone numbers in different formats as an array of strings: phone_numbers = [“213-375-2990”, “(213) 375-5217”, “(213)37 54 574”, “213 375 2869”]. We want to be able to validate that the string is a valid phone number and a regex makes it easy. We can use a method like #any? to check if all of the phone numbers are valid, like so:

phone_numbers.any? {|number| !number.match(/(\D*\d\D*){10}/)}

=> false

This will method is iterating through the array looking for any cases where the phone number does not match the regular expression given. Our regular expression is looking for 10 consecutive cases “{10}” of the pattern “()” zero or more “*” non-digit characters “\D” followed by a number “\d” followed by zero or more non-digit characters. In other words, 10 digits that may or may not have other characters separating them. This method will return false if there are any invalid numbers (less than or more than 10 digits). Now let’s quickly reformat these numbers and store them more neatly. For this, we can use the #map method:

phone_numbers.map! {|number| number.scan(/\d/).join()}

=> [“2133752990”, “2133755217”, “2133754574”, “2133752869”]

This method takes each string (number) in the phone_numbers array and returns an array of characters that match our regex for each string, in this case, any digit characters. The method then joins the array so that we store them as strings in our original array. Remember the #map! method with an exclamation point is destructive so the original array is modified. For more info check out this regex cheat sheet on cheatography or the Ruby documentation.

Leave a Reply