To extract a particular string

On Thu, Sep 19, 2013 at 8:01 PM, Raj pal [email protected] wrote:

Hi Robert,

Can you explain me why this 1 removes the Bracket? I refered the
document but it’s not clear enough to understand. why it removes both
opening and closing bracket here?

From the docs at:

“If a Regexp is supplied, the matching portion of the string is
returned. If a capture follows the regular expression, which may be a
capture group index or name, follows the regular expression that
component of the MatchData is returned instead.”

When you surround some part of the regex in parens, that’s a capturing
group, and by passing the second argument as a 1, you are telling that
you are interested in the first captured group. You can also have
named groups, instead of by index:

a[/[(?[^]]*)]/, :word]

Hope this helps,

Jesus.