Capturing groups
Regular Expression: Understanding Capturing Groups in context of Ruby.
- What does capturing groups means ?
- Are there any “non” capture groups ?
- Named capture groups ?
- What does “back-referencing” means ?
Capture Groups looks likes this : (regexp).
Basically they are represented by “parentheses” and the “pattern” in them are matched against the given string.
example : str = “The sky is blue”
md = (/(blue)/).match(str)
=> #<MatchData “blue” 1:”blue”>
$1 is a scripting shell varibale.
So if you do $1 after the above code.
$1
=> blue.
This is a small example of capturing groups.
If there are capturing groups then are there any non capturing groups ?
There are. In this the “pattern” is given in parentheses but with “?:” is specified before the regexp. These optimize the regular expression. They do not capture anything in them, they just simply match the regexp with the specified string.
Example : str = “The sky is blue”
md = (/(?:blue)/).match(str)
=> #<MatchData “blue”>
Now see the difference between the capture and the non capture.
#This is the example of the capture group.
str = “The sky is blue”
md = (/(blue)/).match(str)
=> #<MatchData “blue” 1:”blue”>
The difference lies in the output of the two. The “blue” of capture group is stored in 1. This looks like the hash “Key-Value” pair.
So when we do $1 we will get “blue”, which is of the capture group.
What are named and numbered capture groups ?
Well named and numbered capture are nothing but a name or a number given to the group of the characters or the pattern that is being matched to the string specified.
Get Ragini’s stories in your inbox
Join Medium for free to get updates from this writer.
str = “The sky is blue”
md = (/(?<character>blue)/).match(str)
=> #<MatchData “blue” character:”blue”>
The string blue now is the value of the key “character”.
But if you try to access “character” it will give and error.
Error :
> “the value is #{md[character]}”
“the value is #{md[character]}”
NameError: undefined local variable or method `character’ for main:Object
from (irb):251
from /usr/bin/irb:11:in `<main>’
But if we do :
irb(main):252:0> “the value is #{md[1]}”
output is : “the value is ‘#{md[1]}’”
=> “the value is ‘the’ ”
For accessing it the $1 is to be used.
OR
irb(main):287:0> (/(?<char>\w)\k<char>/) =~ str
(/(?<char>\w)\k<char>/) =~ str
=> 10
irb(main):288:0> “The character is #{char}”
“The character is #{char}”
=> “The character is l”
We will get this as the output.
Another concept comes is the back-referencing. Backreferencing basically matches the same text again that was matched by the capturing group.
Backreference is there so that the developers can refer back the saved string. They start with the backslash(\) .
They provide a way to identify any repeated character or a substring in a String specified.
Example :
irb(main):361:0> str = “He said Hello”
str = “He said Hello”
=> “He said Hello”
irb(main):362:0> md = (/(?<char>\w)\k<char>/).match(str)
md = (/(?<char>\w)\k<char>/).match(str)
=> #<MatchData “ll” char:”l”>
#The below example is what “md” has captured in it.
irb(main):363:0> md.captures
md.captures
=> [“l”]
#The example here shows the use of \1.
irb(main):366:0> md = (/(\w)\1/).match(str)
md = (/(\w)\1/).match(str)
=> #<MatchData “ll” 1:”l”>
\k and \1 matches the next value that is the same character in the string of the named capturing group and the first capturing group respectively.
So this was small explanation about capturing groups in regular expression.
References :





