Ruby:提取第二个和第三个句号之间的子字符串
Ruby: extract substring between 2nd and 3rd fullstops
我正在 Ruby 中构建一个程序,它需要在字符串中的第二个和第三个句号之间提取值。
我在网上搜索了各种相关的解决方案,包括截断和这个之前的 Stack-Overflow 问题:Get value between 2nd and 3rd comma,但是没有答案说明了 Ruby 语言的解决方案。
提前致谢。
list = my_string.split(".")
list[2]
我想就可以了。第一个命令将它拆分成一个列表。第二得到你想要的位
您可以在 full stops(又名句点)上拆分字符串,但这会创建一个数组,每个子字符串在句号前都有一个元素。如果文档有,比如说,一百万个这样的子字符串,那将是一个相当低效的方法来获得第三个。
假设字符串是:
mystring =<<_
Now is the time
for all Rubiests
to come to the
aid of their
bowling team.
Or their frisbee
team. Or their
air guitar team.
Or maybe something
else...
_
您可以采用以下几种方法。
#1 使用正则表达式
r = /
(?: # start a non-capture group
.*?\. # match any character any number of times, lazily, followed by a full stop
){2} # end non-capture group and perform operation twice
\K # forget everything matched before
[^.]* # match everything up to the next full stop
/xm # extended/free-spacing regex definition mode and multiline mode
mystring[r]
#=> " Or their\nair guitar team"
您当然可以编写正则表达式:
r = /(?:.*?\.){2}\K[^.]*/m
但扩展形式使其成为 self-documenting。
正则表达式引擎将遍历字符串,直到找到匹配项或断定不存在匹配项,然后就此停止。
#2 假装句号是换行符
首先假设我们正在寻找第三行,而不是第三个子字符串后跟一个句号。我们可以这样写:
mystring.each_line.take(3).last.chomp
# => "to come to the"
Enumerable#take determines when a line ends by examining the input record separator, which is held by the global variable$/
。默认情况下,$/
等于换行符。因此我们可以这样做:
irs = $/ # save old value, normally \n
$/ = '.'
mystring.each_line.take(3).last[0..-2]
#=> " Or their\nair guitar team"
那就不留脚印:
$/ = irs
这里 String#each_line returns 一个枚举器(实际上是一个确定值序列的规则),而不是一个数组。
我正在 Ruby 中构建一个程序,它需要在字符串中的第二个和第三个句号之间提取值。
我在网上搜索了各种相关的解决方案,包括截断和这个之前的 Stack-Overflow 问题:Get value between 2nd and 3rd comma,但是没有答案说明了 Ruby 语言的解决方案。
提前致谢。
list = my_string.split(".")
list[2]
我想就可以了。第一个命令将它拆分成一个列表。第二得到你想要的位
您可以在 full stops(又名句点)上拆分字符串,但这会创建一个数组,每个子字符串在句号前都有一个元素。如果文档有,比如说,一百万个这样的子字符串,那将是一个相当低效的方法来获得第三个。
假设字符串是:
mystring =<<_
Now is the time
for all Rubiests
to come to the
aid of their
bowling team.
Or their frisbee
team. Or their
air guitar team.
Or maybe something
else...
_
您可以采用以下几种方法。
#1 使用正则表达式
r = /
(?: # start a non-capture group
.*?\. # match any character any number of times, lazily, followed by a full stop
){2} # end non-capture group and perform operation twice
\K # forget everything matched before
[^.]* # match everything up to the next full stop
/xm # extended/free-spacing regex definition mode and multiline mode
mystring[r]
#=> " Or their\nair guitar team"
您当然可以编写正则表达式:
r = /(?:.*?\.){2}\K[^.]*/m
但扩展形式使其成为 self-documenting。
正则表达式引擎将遍历字符串,直到找到匹配项或断定不存在匹配项,然后就此停止。
#2 假装句号是换行符
首先假设我们正在寻找第三行,而不是第三个子字符串后跟一个句号。我们可以这样写:
mystring.each_line.take(3).last.chomp
# => "to come to the"
Enumerable#take determines when a line ends by examining the input record separator, which is held by the global variable$/
。默认情况下,$/
等于换行符。因此我们可以这样做:
irs = $/ # save old value, normally \n
$/ = '.'
mystring.each_line.take(3).last[0..-2]
#=> " Or their\nair guitar team"
那就不留脚印:
$/ = irs
这里 String#each_line returns 一个枚举器(实际上是一个确定值序列的规则),而不是一个数组。