如何在:和字符串之间获取文本?

时间:2022-04-22 21:47:20

I have a bunch of Strings in an array in the form:

我有一串字符串在数组中的形式:

["name: hi", "pw: lol"]

["名称:嗨”、“pw:lol”)

How can I extract just the portion after the semi-colon and space in Ruby?

如何提取Ruby中的分号和空格后面的部分?

5 个解决方案

#1


7  

["name: hi", "pw: lol"].map{|x| x.split(': ')[1]}

produces:

生产:

["hi", "lol"]

#2


4  

The suggestions by Garrett and Peter will definitely do the trick. However, if you want, you can go a step further and easily turn this into a hash.

加勒特和彼得的建议一定会奏效。但是,如果您愿意,您可以更进一步,轻松地将其转换为散列。

values = ["name: hi", "pw: lol"]
hash = Hash[*values.map{|item| item.split(/\s*:\s*/)}.flatten]
# => {"name"=>"hi", "pw"=>"lol"}

There's a lot packed into the second line so let me point out a few improvements:

第二行包含了很多内容,让我指出一些改进:

  • The split allows for flexibility in the colon, allowing for any number of spaces both before and after.
  • 分割允许在冒号中灵活,允许在之前和之后的任意数量的空格。
  • After the map call we have the array [["name", "hi"], ["pw", "lol"]]
  • 在映射调用之后,我们有数组[["name", "hi"], ["pw", "lol"]
  • Hash#[] takes a list of values that will be mapped as key, value, key, value,... As a result, we need to flatten the mapped array to pass to Hash#[]
  • 哈希#[]列出将被映射为键、值、键、值的值的列表。因此,我们需要将映射数组简化为散列#[]

Since I don't know your exact needs I can't say whether you want a Hash or not, but it's nice to have the option.

因为我不知道你的确切需求,我不能说你是否想要散列,但是有这个选项很好。

#3


1  

You can loop through them and split them up by the : like so:

你可以对它们进行循环并将它们分割成如下:

["name: hi", "pw: lol"].each do |item|
    puts item.split(":").last.lstrip
end

Example:

例子:

>> a = ["name: hi", "pw: lol"]
=> ["name: hi", "pw: lol"]
>> a.each do |item|
?> puts item.split(":").last.lstrip
>> end

>> hi
>> lol

#4


1  

I suggest you use Regular Expressions to process strings, although the previous answers work

我建议您使用正则表达式来处理字符串,尽管前面的答案是有效的

a = ["name: hi", "pw: lol"]

a = ["name: hi", "pw: lol"]

a.map{|item| item.match(/\w+: ([\w\s]+)/)[1]}

一个。地图{ |项目|项目。匹配(/ \ w +:(\ w \[s]+)/)[1]}

this would output:

这将输出:

=> ["hi", "lol"]

= >(“你好”、“lol”)

#5


0  

a.to_s
     h=hi~=a

a.index[h].value

or

hi{1}

#1


7  

["name: hi", "pw: lol"].map{|x| x.split(': ')[1]}

produces:

生产:

["hi", "lol"]

#2


4  

The suggestions by Garrett and Peter will definitely do the trick. However, if you want, you can go a step further and easily turn this into a hash.

加勒特和彼得的建议一定会奏效。但是,如果您愿意,您可以更进一步,轻松地将其转换为散列。

values = ["name: hi", "pw: lol"]
hash = Hash[*values.map{|item| item.split(/\s*:\s*/)}.flatten]
# => {"name"=>"hi", "pw"=>"lol"}

There's a lot packed into the second line so let me point out a few improvements:

第二行包含了很多内容,让我指出一些改进:

  • The split allows for flexibility in the colon, allowing for any number of spaces both before and after.
  • 分割允许在冒号中灵活,允许在之前和之后的任意数量的空格。
  • After the map call we have the array [["name", "hi"], ["pw", "lol"]]
  • 在映射调用之后,我们有数组[["name", "hi"], ["pw", "lol"]
  • Hash#[] takes a list of values that will be mapped as key, value, key, value,... As a result, we need to flatten the mapped array to pass to Hash#[]
  • 哈希#[]列出将被映射为键、值、键、值的值的列表。因此,我们需要将映射数组简化为散列#[]

Since I don't know your exact needs I can't say whether you want a Hash or not, but it's nice to have the option.

因为我不知道你的确切需求,我不能说你是否想要散列,但是有这个选项很好。

#3


1  

You can loop through them and split them up by the : like so:

你可以对它们进行循环并将它们分割成如下:

["name: hi", "pw: lol"].each do |item|
    puts item.split(":").last.lstrip
end

Example:

例子:

>> a = ["name: hi", "pw: lol"]
=> ["name: hi", "pw: lol"]
>> a.each do |item|
?> puts item.split(":").last.lstrip
>> end

>> hi
>> lol

#4


1  

I suggest you use Regular Expressions to process strings, although the previous answers work

我建议您使用正则表达式来处理字符串,尽管前面的答案是有效的

a = ["name: hi", "pw: lol"]

a = ["name: hi", "pw: lol"]

a.map{|item| item.match(/\w+: ([\w\s]+)/)[1]}

一个。地图{ |项目|项目。匹配(/ \ w +:(\ w \[s]+)/)[1]}

this would output:

这将输出:

=> ["hi", "lol"]

= >(“你好”、“lol”)

#5


0  

a.to_s
     h=hi~=a

a.index[h].value

or

hi{1}