Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using more than 9 unnamed captures in Ruby Regex

Tags:

regex

ruby

I have a long string formatted like a log entry and am looking to get several captures from it and store them in different variables.

I know Ruby stores the first 9 captures into the variables $1...$9, does it also store other captures in $10...$99?

If not what would be a simple way to do this?

like image 541
Max Burns Avatar asked Dec 09 '22 20:12

Max Burns


1 Answers

String#match returns MatchData object which holds an array of captures.

m = Regexp.new('(.)' * 20).match('The quick brown fox jumps over the lazy dog')
m[12]
# => "r" 

Although $12 also works:

> $12
# => "r" 
like image 69
Mladen Jablanović Avatar answered Dec 26 '22 03:12

Mladen Jablanović