Ruby正则表达式提取第一个(和最后一个)之间的字符串



我有一个字符串:

"whatever( here i com  ( asdfasDF ( ) ) ) go home"

我想提取第一个"("和最后一个")"之间的所有内容,即:

" here i com  ( asdfasDF ( ) ) "

什么是一个好的正则表达式来提取它?


更新:

如果我的字符串包含换行符,则以下操作有效:

/((([.|s|S])*))/

带有捕获的贪婪表达式,例如:

/((.*))/
"whatever( here i com  ( asdfasDF ( ) ) ) go home"[/((.*))/m, 1]

"whatever( here i com  ( asdfasDF ( ) ) ) go home"[/(?<=().*(?=))/m]
str = "whatever( here i com ( asdfasDF ( ) ) ) go home"
p str[str.index("(")+1..str.rindex(")")-1]

最新更新