In Progress
Unit 1, Lesson 1
In Progress

String Subscript Regex

Video transcript & code

Let's say we've got a string containing an episode title:

'085 Ignore Arguments'

From this title we'd like to extract just the episode number. We could do this with a regular expression match that looks for three digits at the start of the string:

'085 Ignore Arguments'.match(/^\d{3}/) # => #<MatchData "085">

Once we have a MatchData object, we just need to ask it for the whole matched string, which is always the "zero-th" match group:

'085 Ignore Arguments'.match(/^\d{3}/)[0] # => "085"

This is a lot to type to get at the episode number though. For this simple case, Ruby gives us a shorthand. We can pass a regex to the subscript operator (otherwise known as the square brackets), and the return value will be the part of the string that matched.

'085 Ignore Arguments'[/^\d{3}/] # => "085"

Now let's say we decide we want to extract the episode name, rather than the number. So we add a group to the regex which will capture only the part of the string after the episode number and a space. But the return value is the string matched by the entire regex—which in this case is the entire string.

'085 Ignore Arguments'[/^\d{3} (.+)/] # => "085 Ignore Arguments"

We just want the part that matched the parenthesized group. Fortunately, the String subscript operator can accommodate us. It takes an optional second argument, which specifies the match group number to return. So we can tell it to give us the first match group, and it dutifully returns just the episode name.

'085 Ignore Arguments'[/^\d{3} (.+)/, 1] # => "Ignore Arguments"

String subscripts can take lots of other arguments as well, but we'll save that for another day. Happy hacking!

Responses