REGEX with ruby string

why would
hello[/aeiou(.)\1/, 2] give nil
but
hello[/aeiou(.)\1/] give ‘ell’

shouldn’t the answer to the latter be ‘el’ for the first to be correct?

example taken from Rubydocs

1 Like

I have not found exact match for the code you posted in the linked ruby docs, so I will assume that we talk about this part:

a = "hello there"

a[/[aeiou](.)\1/]      #=> "ell"
a[/[aeiou](.)\1/, 0]   #=> "ell"
a[/[aeiou](.)\1/, 1]   #=> "l"
a[/[aeiou](.)\1/, 2]   #=> nil

Let’s start by breaking down the regular expression /[aeiou](.)\1/:

  1. [aeiou] matches a single character in the list aeiou
  2. (.) matches any character (except for line terminators) and creates capturing group (parentheses)
  3. \1 matches the same text as the 1st capturing group ((.))

So we are looking for three characters in the "hello there" where the first character is a, e, i, o or u and the second character must be repeated two times. The only part of the "hello there" that matches this description is "ell", so this is our full match.


a[/[aeiou](.)\1/] does not have the capture parameter, so it takes default value - 0, which means that we want to get the full match, in our case this is "ell" and this value was returned.

a[/[aeiou](.)\1/, 0] same as previous, capture parameter is 0, which means that we want to get the full match, in our case this is "ell" and this value was returned.

a[/[aeiou](.)\1/, 1] has the capture parameter equal to 1, which means that we want go get only the text matched by the first capturing group ((.)), in our case this is "l" and this value was returned.

a[/[aeiou](.)\1/, 2] has the capture parameter equal to 2, which means that we want go get only the text matched by the second capturing group, but in case of our regular expression there was only one capturing group, so no matter what text we use we will never get a value for this expression, so the result is nil and this value was returned.


I hope this will help a bit :slight_smile:

1 Like