Get content between parenthesis from String object in Ruby

regex, ruby, string

Solution

You can use String#[] with a regular expression:

a = "Hi my name is John (aka Johnator)"
a[/\(.*?\)/]
# => "(aka Johnator)"

Problem

I have a string like this: `Hi my name is John (aka Johnator)`. What is the best way to get what comes between the parentheses (including the parentheses)?

Original source

Related problems