当前位置: 动力学知识库 > 问答 > 编程问答 >

string - Ruby: extract substring between 2nd and 3rd fullstops

问题描述:

i am constructing a program in Ruby which requires the value to be extracted between the 2nd and 3rd full-stop in a string.

I have searched online for various related solutions, including truncation and this prior Stack-Overflow question: Get value between 2nd and 3rd comma, however no answer illustrated a solution in the Ruby language.

Thanks in Advance.

网友答案:
list = my_string.split(".")
list[2]

That will do it I think. First command splits it into a list. Second gets the bit you want

网友答案:

You could split the string on full stops (aka periods), but that creates an array with one element for each substring preceding a full stop. If the document had, say, one million such substrings, that would be a rather inefficient way of getting just the third one.

Suppose the string is:

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...
_

Here are a couple of approaches you could take.

#1 Use a regular expression

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"

You could of course write the regex:

r = /(?:.*?\.){2}\K[^.]*/m

but the extended form makes it self-documenting.

The regex engine will step through the string until it finds a match or concludes that there can be no match, and stop there.

#2 Pretend a full stop is a newline

First suppose we were looking for the third line, rather than the third substring followed by a full stop. We could write:

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 $/. By default, $/ equals a newline. We therefore could do this:

irs = $/  # save old value, normally \n
$/ = '.'
mystring.each_line.take(3).last[0..-2]
  #=> " Or their\nair guitar team"

Then leave no footprints:

$/ = irs

Here String#each_line returns an enumerator (in effect, a rule for determining a sequence of values), not an array.

分享给朋友:
您可能感兴趣的文章:
随机阅读: