Ruby:如何解析字符串以提取某些内容并将其分配给变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8013421/
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 04:27:29  来源:igfitidea点击:

Ruby: How to parse a string to pull something out and assign it to a variable

ruby

提问by Andrew

I have a string that looks something like this:

我有一个看起来像这样的字符串:

"my name is: andrew"

I'd like to parse the string, pull out the name from the string, and assign it to a variable. How would I do this with Ruby?

我想解析字符串,从字符串中提取名称,并将其分配给一个变量。我将如何用 Ruby 做到这一点?

Update:

更新:

The string I used as an example was only an example. The strings that I will be working with can change formats, so you can't rely on the colon being in the actual example. Here are a few examples that I'm working with:

我用作示例的字符串只是一个示例。我将使用的字符串可以更改格式,因此您不能依赖实际示例中的冒号。以下是我正在使用的一些示例:

"/nick andrew"     # command: nick, value: "andrew"
"/join developers" # command: join, value: "developers"
"/leave"           # command: leave, value: nil

I'd like to use some sort of regular expression to solve this (since the string can change formats), rather than splitting the string on certain characters or relying on a certain character position number.

我想使用某种正则表达式来解决这个问题(因为字符串可以改变格式),而不是将字符串拆分为某些字符或依赖某个字符位置编号。

采纳答案by Andrew

This tutorialreally helped me understand how to work with regular expressions in Ruby.

本教程确实帮助我了解了如何在 Ruby 中使用正则表达式。

One way to use a regular expression to get the string you want is to replace the stuff you don't want with an empty string.

使用正则表达式获取所需字符串的一种方法是将不需要的内容替换为空字符串。

original_string = "my name is: andrew"
name = original_string.sub(/^my name is: /, '') # => 'andrew'

another_format = "/nick andrew"
name = another_format.sub(/^\/nick /, '') # => 'andrew'

However, that's just string replacement/substitution. The regex is not capturing anyting.

但是,这只是字符串替换/替换。正则表达式没有捕获任何内容。

To capture a string using a regular expression, you can use the Ruby matchmethod:

要使用正则表达式捕获字符串,您可以使用 Rubymatch方法:

original_string = "my name is: andrew"
matches = original_string.match /^my name is: (.*)/
name = matches[1] # return the first match

回答by Sahil Muthoo

s = "my name is: andrew"
p s.split(':')[1].strip # "andrew"

See

  1. split
  2. strip
  1. split
  2. strip

回答by Sean Vikoren

Another way:

其它的办法:

name = "my name is: andrew".split(/: */)[1] # => "andrew"

or name = "my name is: andrew".split(/: */).last # => "andrew"

或 name = "我的名字是:andrew".split(/: */).last # => "andrew"


Breaking it down, first we break it into parts. The regular expression /: */ says a : followed by any number of spaces will be our splitter.


分解它,首先我们把它分成几部分。正则表达式 /: */ 表示 : 后跟任意数量的空格将是我们的分隔符。

"my name is: andrew".split(/: */) # => ["my name is", "andrew"]

Then we select the second item:

然后我们选择第二项:

["my name is", "andrew"][1] # => "andrew" 

回答by Zabba

One way to do that is:

一种方法是:

s = "my name is: andrew"
pos = (s =~ /(?!.*:).*/)
result = s[pos..-1]
p result.strip!  # "andrew"

Another:

其他:

s = "my name is: andrew";
p s.slice(s.index(":")..-1) # "andrew"

回答by DigitalRoss

s.split.last

That should work with all of your cases.

这应该适用于您的所有情况。