Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提取第一个(和最后一个)之间字符串的Ruby正则表达式_Ruby_Regex - Fatal编程技术网

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

提取第一个(和最后一个)之间字符串的Ruby正则表达式,ruby,regex,Ruby,Regex,我有一个字符串: "whatever( here i com ( asdfasDF ( ) ) ) go home" 我想提取第一个“(“和最后一个”)之间的所有内容,即: " here i com ( asdfasDF ( ) ) " 什么是好的正则表达式来提取它 更新: 如果我的字符串包含换行符,则以下操作有效: /\((([.|\s|\S])*)\)/ 带有捕获的贪婪表达式,例如: /\((.*)\)/ 或 “随便什么(这里是我的com(asdfasDF()))回家”[/(?

我有一个字符串:

"whatever( here i com  ( asdfasDF ( ) ) ) go home"
我想提取第一个
“(“
和最后一个
”)之间的所有内容,即:

" here i com  ( asdfasDF ( ) ) "
什么是好的正则表达式来提取它


更新:

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

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

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

/\((.*)\)/


“随便什么(这里是我的com(asdfasDF()))回家”[/(?你试过运行一些基本的正则表达式教程吗?非常适合测试正则表达式。“你的字符串”?谁的字符串?你不是写了“让我说我有一个字符串”吗?你能做
str[str.index(“”+1…str.rindex(“”)-1]
?@JanDvorak优秀的帖子提示:)根据您的要求完成…喜欢:)事实上,我混淆了
..
(右排)和
(包括)。我指的是后者。我刚刚测试了(我希望您会)当我的字符串中有新行字符时,这会中断
str = "whatever( here i com ( asdfasDF ( ) ) ) go home"
p str[str.index("(")+1..str.rindex(")")-1]
"whatever( here i com  ( asdfasDF ( ) ) ) go home"[/\((.*)\)/m, 1]
"whatever( here i com  ( asdfasDF ( ) ) ) go home"[/(?<=\().*(?=\))/m]