When defining a regular expression without capturing groups, scan
returns an array of strings, where each line represents a regular expression match. If you use scan(/P(?:erl|ython)/)
(which matches your regular expression, with the exception of capture groups), you will get ["Perl", "Python"]
what you expect.
However, if a regular expression with capture groups is specified, scan
will return an array of arrays, where each sub-array contains captures of a given match. Therefore, if you have, for example, regex (\w*):(\w*)
, you will get an array of arrays in which each sub-array contains two lines: the part before the colon and the part after the colon. And in your example, each sub-array contains one line: the part matched (erl|ython)
.
source share