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)?
You can use String#[] with a regular expression:
a = "Hi my name is John (aka Johnator)"
a[/\(.*?\)/]
# => "(aka Johnator)"
Use [^()]*?
for select text in parenthese :
a = "Hi (a(b)c) ((d)"
# => "Hi (a(b)c) ((d)"
a.gsub(/\([^()]*?\)/) { |x| p x[1..-2]; "w"}
"b"
"d"
# => "Hi (awc) (w"
Try this:
str1 = ""
text = "Hi my name is John (aka Johnator)"
text.sub(/(\(.*?\))/) { str1 = $1 }
puts str1
Edit: Didn't read about leaving the parenthesis!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With