# Regular Expressions in Ruby

*Published on December 26, 2022*

---


In my programming career so far, I've always used regular expressions on a need-to-know basis, and never really took the time to learn them properly. Now that I've been programming in Ruby for the past year and a half (and really enjoying it), I decided to take a deeper look and understand&nbsp;_the Ruby way_&nbsp;to do them properly.

So I spent the Christmas weekend reading and playing with regular expressions in Ruby. It was time well spent. Ruby makes them much less intimidating, as compared with C# and JavaScript, and fun too. I've learned a lot, and can use those learnings not only when writing Rails apps, but also to improve my productivity by using them in VS Code's search-find-replace feature.

What follows are my notes from the weekend learning session. I hope you find them useful and you learn something new. &nbsp;

* * *

**What is a regular expression?**  
  
A regular expression is a pattern describing the contents of a string. Regular expressions offer a very powerful way to match, search, and replace a pattern of characters in text.  
  
**Why do we need them?**

1. To test if a string contains a given pattern
2. To extract the portions matching the given pattern.

**How to create them?**

Ruby offers three syntaxes to create a regular expression object.

```ruby
pattern_one = /hello/ 
pattern_two = %r{hello} 
pattern_three = Regexp.new("hello")
```

They all create an instance of the&nbsp;[`Regexp`](https://ruby-doc.org/core-3.1.1/Regexp.html)&nbsp;class. Personally, I like the first syntax as it keeps the code concise. You can think of the slashes (/) as the quotes around strings.

```ruby
puts pattern_one.class   # Regexp 
puts pattern_two.class   # Regexp 
puts pattern_three.class # Regexp
```

You can pattern match using the&nbsp;`=~`&nbsp;operator or the&nbsp;`Regexp#match`&nbsp;method.  
  
**=~ operator**  
  
The&nbsp;`=~`&nbsp;operator returns the first position at which the given regular expression pattern occurs. If the pattern doesn't match, it returns&nbsp;`nil`.

```ruby
/my/ =~ "Hi, my name is Akshay. This is my blog." 
=> 4 

/my/ =~ "Hi, I am Akshay" 
=> nil
```

This operator works the same for both&nbsp;`String`&nbsp;and&nbsp;`Regexp`&nbsp;so order doesn't matter.  
  
**MatchData**  
  
The other way to match a string for patterns is to use a&nbsp;[`MatchData`](https://ruby-doc.org/core-3.1.1/MatchData.html)&nbsp;object. It encapsulates the result of matching a regular expression against a string.  
  
Calling the&nbsp;[_`match`_](https://ruby-doc.org/core-3.1.1/MatchData.html#method-i-match)&nbsp;method on a&nbsp;`Regexp`&nbsp;and a&nbsp;`String`&nbsp;returns an instance of&nbsp;`MatchData`.

```ruby
line = "this is my blog" 

line.match(/my/) 
=> #<MatchData "my"> 

/my/.match(line) 
=> #<MatchData "my">
```

`MatchData`&nbsp;acts as an array. The first element is the matched string.  
  
In this example, the match is&nbsp;`my`&nbsp;since the&nbsp;`.`&nbsp;matches a single character.

```ruby
md = /m./.match("this is my blog") 

md[0] 
=> "my"
```

You can access the matched string by calling&nbsp;`to_s`&nbsp;on the&nbsp;`MatchData`.

```ruby
md.to_s 
=> "my"
```

The subsequent values in the array contain the stored values between brackets.

```ruby
md = /age: (\d+)/.match("age: 31 years") 

md 
=> #<MatchData "age: 31" 1:"31"> 

md[0] 
=> "age: 31" 

md[1] 
=> "31"
```

The&nbsp;[`captures`](https://ruby-doc.org/core-3.1.1/MatchData.html#method-i-captures)&nbsp;method returns the array of captured values.

```ruby
line = "age: 31 location: victoria" 
pattern = /age: (\d+) location: (\w+)/ 

md = pattern.match(line) 
=> #<MatchData "age: 31 location: victoria" 1:"31" 2:"victoria"> 

md.captures 
=> ["31", "victoria"]
```

**How to check if the text matches a pattern?**

Use the&nbsp;[`match?`](https://ruby-doc.org/core-3.1.1/Regexp.html#method-i-match-3F)&nbsp;method to indicate if the regular expression pattern is matched or not.

```ruby
/R/.match? "Ruby"
=> true

/R/.match? "ruby"
=> false
```

## Metacharacters

These characters have a special meaning in regular expressions. I've put them in a separate line as using a comma to separate them always confused me.

```javascript
( 
) 
[ 
] 
{ 
} 
. 
? 
+ 
*
```

To escape them, use a backslash&nbsp;`\`.

```ruby
/1 + 2/ =~ "expression: 1 + 2" 
=> nil 

/1 \+ 2/ =~ "expression: 1 + 2" 
=> 12
```

## Character Classes

A character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.

Use character classes to tell the regex engine to match only one out of several characters.

```ruby
/[bcr]at/.match("cat") 
=> #<MatchData "cat"> 

/[bcr]at/.match("that") 
=> nil 

/gr[ae]y/.match("gray") 
=> #<MatchData "gray"> 

/gr[ae]y/.match("grey") 
=> #<MatchData "grey"> 

/gr[ae]y/.match("gruy") 
=> nil
```

Some important points about character classes:

- A character class only matches a single character.
- The order of characters inside the brackets doesn't matter.

For one or two characters, it's enough to list them in the character classes.&nbsp;**What if there are many characters**? For example, if you want to match all letters from \`d-m\`, writing each letter could get tedious.  
  
In this case, use a range as follows:

```ruby
md = /[d-m]at/.match("mat") 
=> #<MatchData "mat"> 

md = /[d-m]at/.match("cat") 
=> nil 

# /[a-d]/ is the same as /abcd/
```

To match all characters except those listed in the brackets, place a&nbsp;`^`&nbsp;at the beginning. This is known as&nbsp;_**negation**_. The result is that the character class matches any character that is&nbsp;_not_&nbsp;in the character class.

```ruby
md = /[^bcr]at/.match("hat") 
=> #<MatchData "hat"> 
```

A number of common character groups have their own built-in shortcuts, called meta-characters.

- `/./`&nbsp;- Any character except a newline.
- `/./m`&nbsp;- Any character (the m modifier enables multiline mode)
- `/\w/`&nbsp;- A word character&nbsp;`[a-zA-Z0-9_]`
- `/\W/`&nbsp;- A non-word character&nbsp;`[^a-zA-Z0-9_]`.
- `/\d/`&nbsp;- A digit character&nbsp;`[0-9]`
- `/\D/`&nbsp;- A non-digit character&nbsp;`[^0-9]`
- `/\s/`&nbsp;- A whitespace character&nbsp;`/[\t\r\n\f\v]/`
- `/\S/`&nbsp;- A non-whitespace character:&nbsp;`/[^ \t\r\n\f\v]/`

## Repetition

So far, we've matched a single character. What if we want to match multiple characters—a sequence of one or more characters?

```ruby
/\d/.match("37signals") 
=> #<MatchData "3">
```

You can use a repetition meta-character to specify how many times the matched character needs to occur. For example, the meta-character (+) matches the character one or more times.

```ruby
/\d+/.match("37signals") 
=> #<MatchData "37">
```

Here is a list of available meta-characters.

- `*`&nbsp;- Zero or more times
- `+`&nbsp;- One or more times
- `?`&nbsp;- Zero or one times (optional)
- `{`_`n`_`}`&nbsp;- Exactly&nbsp;_`n`_&nbsp;times
- `{`_`n`_`,}`&nbsp;-&nbsp;_`n`_&nbsp;or more times
- `{,`_`m`_`}`&nbsp;-&nbsp;_`m`_&nbsp;or less times
- `{`_`n`_`,`_`m`_`}`&nbsp;- At least&nbsp;_`n`_&nbsp;and at most&nbsp;_`m`_&nbsp;times

* * *

Alright, that's all for now. I will continue learning more about regular expressions this week, especially how to capture and group pattern-matched data. So expect a second post in this series soon.

Hope you found this article useful and learned something new.

Merry Christmas,

Akshay

![christmas-tree.png](https://writesoftwarewell.com/media/8fylxckve6u2stfcv42iws0x8ddm.png "Merry Christmas!")

