Take the String:"The only true (wisdom) is in knowing you know (nothing)"
I want to extract nothing
.
拿绳子来说:“唯一的真理(智慧)就是知道自己什么都不知道。”我什么也不想提取。
What I know about it:
我所知道的:
- It will always be inside a parenthesis
- 它总是在括号里面
- The parenthesis will always be the last element before the line-end:
$
- 括号始终是行尾:$之前的最后一个元素
I first attempted to match it with /\(.*\)$/
, but that obviously returned(wisdom) is in knowing you know (nothing)
.
我第一次尝试将它与/\(.*\)$/匹配,但显然回报(智慧)是知道你什么都不知道。
6 个解决方案
#1
4
You want to use negative character group matching, like [^...]
:
你想使用消极的角色组匹配,就像[^…]:
s = 'The only true (wisdom) is in knowing you know (nothing)'
s.match(/\(([^)]+)\)$/).captures
Debuggex演示
In this case, nothing
is in the first sub-group match, but the entire regex technically matches (nothing)
. To match exactly nothing
as the entire match, use:
在这种情况下,第一个子组匹配中没有任何内容,但是整个regex技术上是匹配的(没有)。若要与整个匹配完全不匹配,请使用:
s = 'The only true (wisdom) is in knowing you know (nothing)'
s.match(/(?<=\()([^)]+)(?=\)$)/).captures
Debuggex演示
#2
1
I would do
我要做
s = 'The only true (wisdom) is in knowing you know (nothing)'
s.match(/\(([^)]+)\)$/).captures # => ["nothing"]
#3
0
You could use scan
to find all matches and then take the last one:
你可以用扫描找到所有的匹配,然后取最后一个:
str = "The only true (wisdom) is in knowing you know (nothing)"
str.scan(/\((.+?)\)/).last
#=> "nothing"
#4
0
You can use the \z which matches end of string. try
您可以使用\z来匹配字符串的结束。试一试
\([a-z]+\)\z
\[a - z]+ \ \ z
Way simpler and will ignore everything else but what you need.
更简单,会忽略其他一切,除了你需要的。
Test it here:
测试在这里:
http://rubular.com/
#5
0
It's even trickier if there's any chance of nesting. In that case you need some recursion:
如果有任何嵌套的机会,那就更麻烦了。在这种情况下,你需要一些递归:
"...knowing you know ((almost) nothing)"[/\(((?:[^()]*|\(\g<1>\))*)\)$/, 1]
#=> "(almost) nothing"
#6
0
Look ma, no regex!
看哪,没有正则表达式!
s = 'The only true (wisdom) is in knowing you know (nothing)'
r = s.reverse
r[(r.index(')') + 1)...(r.index('('))].reverse
#=> "nothing"
#1
4
You want to use negative character group matching, like [^...]
:
你想使用消极的角色组匹配,就像[^…]:
s = 'The only true (wisdom) is in knowing you know (nothing)'
s.match(/\(([^)]+)\)$/).captures
Debuggex演示
In this case, nothing
is in the first sub-group match, but the entire regex technically matches (nothing)
. To match exactly nothing
as the entire match, use:
在这种情况下,第一个子组匹配中没有任何内容,但是整个regex技术上是匹配的(没有)。若要与整个匹配完全不匹配,请使用:
s = 'The only true (wisdom) is in knowing you know (nothing)'
s.match(/(?<=\()([^)]+)(?=\)$)/).captures
Debuggex演示
#2
1
I would do
我要做
s = 'The only true (wisdom) is in knowing you know (nothing)'
s.match(/\(([^)]+)\)$/).captures # => ["nothing"]
#3
0
You could use scan
to find all matches and then take the last one:
你可以用扫描找到所有的匹配,然后取最后一个:
str = "The only true (wisdom) is in knowing you know (nothing)"
str.scan(/\((.+?)\)/).last
#=> "nothing"
#4
0
You can use the \z which matches end of string. try
您可以使用\z来匹配字符串的结束。试一试
\([a-z]+\)\z
\[a - z]+ \ \ z
Way simpler and will ignore everything else but what you need.
更简单,会忽略其他一切,除了你需要的。
Test it here:
测试在这里:
http://rubular.com/
#5
0
It's even trickier if there's any chance of nesting. In that case you need some recursion:
如果有任何嵌套的机会,那就更麻烦了。在这种情况下,你需要一些递归:
"...knowing you know ((almost) nothing)"[/\(((?:[^()]*|\(\g<1>\))*)\)$/, 1]
#=> "(almost) nothing"
#6
0
Look ma, no regex!
看哪,没有正则表达式!
s = 'The only true (wisdom) is in knowing you know (nothing)'
r = s.reverse
r[(r.index(')') + 1)...(r.index('('))].reverse
#=> "nothing"