0

I have a string:

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

I want to extract everything between the first "(" and last ")" which is:

" here i com  ( asdfasDF ( ) ) "

What is a good regular expression to extract that?


Update:

If my string contains newline chars the following works:

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

3 回答 3

4

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

/\((.*)\)/
于 2013-04-28T13:27:02.607 回答
3
"whatever( here i com  ( asdfasDF ( ) ) ) go home"[/\((.*)\)/m, 1]

或者

"whatever( here i com  ( asdfasDF ( ) ) ) go home"[/(?<=\().*(?=\))/m]
于 2013-04-28T13:51:18.273 回答
2
str = "whatever( here i com ( asdfasDF ( ) ) ) go home"
p str[str.index("(")+1..str.rindex(")")-1]
于 2013-04-28T13:42:12.923 回答