ruby 在Ruby中的第一个=符号之后获取子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14073743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:37:17  来源:igfitidea点击:

Get substring after the first = symbol in Ruby

rubystring

提问by at.

Purely out of curiosity, is there a more elegant way to simply get the substring after the first =symbol in a string? The following works to give back name=bob:

纯粹出于好奇,是否有更优雅的方法来简单地获取字符串中第一个=符号之后的子字符串?以下工作回馈name=bob

string = "option=name=bob"
string[string.index('=')+1..-1]

It just doesn't feel very Ruby. This also works:

只是感觉不是很红宝石。这也有效:

string.split('=', 2)[1]

Again, not very elegant especially since splitis doing extra unnecessary work. Are regular expressions the answer? I felt this was a little overkill for the simplicity of finding a single character position in a string:

同样,不是很优雅,尤其是因为split做了额外的不必要的工作。正则表达式是答案吗?我觉得这对于在字符串中查找单个字符位置的简单性来说有点矫枉过正:

string.match('=(.*)')[1]

I have to imagine this is an extremely common situation, isn't there a string.after('=')type method? Does Ruby on Rails provide something like this given the frequency this kind of operation is used over the web?

我不得不想象这是一种非常普遍的情况,不是有string.after('=')类型方法吗?鉴于这种操作在网络上使用的频率,Ruby on Rails 是否提供类似的功能?

UPDATE: Forgot to mention the situation when the symbol is not found, nilor empty string should be returned. But the regular expression mechanism and .indexmethod require an extra check for that (so less elegant).

更新:忘记提及找不到符号的情况,nil或者应该返回空字符串。但是正则表达式机制和.index方法需要对此进行额外检查(因此不太优雅)。

回答by dimuch

Not exactly .after, but quite close to:

不完全是.after,但非常接近:

string.partition('=').last

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition

回答by Thomas

There's also this way:

还有这个方法:

string.partition('=')[2]

And this one:

和这个:

string.sub(/.*?=/, '')

I think I prefer the regexp way you mentioned, though.

不过,我想我更喜欢你提到的正则表达式方式。

回答by Brian Ustas

Probably not the Ruby-way (it's a bit cryptic), but you could do this:

可能不是 Ruby 方式(它有点神秘),但你可以这样做:

string[/=/]
$'
=> "name=bob"

or

或者

/=/ =~ string
$'
=> "name=bob"

$'is a global holding the string aftera successful match. It's nilif nothing is matched, too!

$'成功匹配保存字符串的全局变量。这是nil如果没有匹配的新闻,太!

回答by Todd A. Jacobs

Use String#match

使用字符串#match

You can use a regular expression with positive lookbehind to find your match. For example:

您可以使用正向后视的正则表达式来查找匹配项。例如:

string = "option=name=bob"
string.match /(?<==).*/
# => #<MatchData "name=bob">

Use Match Variables to Access Result

使用匹配变量访问结果

Even if you haven't assigned the match data to a variable, Ruby will store it in special match variablesfor you.

即使您尚未将匹配数据分配给变量,Ruby 也会为您将其存储在特殊的匹配变量中。

$&
# => "name=bob"

回答by Gaurav Sachdeva

split(char)is another function which can be used. For instance, we want to get substring before char ':' from "answer:computer" then, we can use "answer:computer".split(':')[0].So, we would get result as "answer".

split(char)是另一个可以使用的功能。例如,我们想从 "answer:computer" 中获取字符 ':' 之前的子字符串,然后我们可以使用"answer:computer".split(':')[0].So,我们将得到结果为 "answer"。