如何在Ruby中返回两个字符串之间的字符串子字符串?

时间:2020-12-08 19:21:37

How would I return the string between two string markers of a string in Ruby?

如何在Ruby中的两个字符串标记之间返回字符串?

For example I have:

例如我有:

  • input_string
  • input_string
  • str1_markerstring
  • str1_markerstring
  • str2_markerstring
  • str2_markerstring

Want to do something like:

想做一些事情,比如:

input_string.string_between_markers(str1_markerstring, str2_markerString)

Example text:

示例文本:

1.9.3-p0 :020 >   s
 => "Charges for the period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:<br>\nAny Network Cap remaining: $366.550<br>International Cap remaining: $0.000"
1.9.3-p0 :021 > str1_markerstring
 => "Charges for the period"
1.9.3-p0 :022 > str2_markerstring
 => "Any Network Cap"
1.9.3-p0 :023 > s[/#{str1_markerstring}(.*?)#{str2_markerstring}/, 1]
 => nil  # IE DIDN'T WORK IN THIS CASE

2 个解决方案

#1


61  

input_string = "blahblahblahSTARTfoofoofooENDwowowowowo"
str1_markerstring = "START"
str2_markerstring = "END"

input_string[/#{str1_markerstring}(.*?)#{str2_markerstring}/m, 1]
#=> "foofoofoo"

or to put it in a method:

或者用一种方法:

class String
  def string_between_markers marker1, marker2
    self[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
  end
end

"blahblahblahSTARTfoofoofooENDwowowowowo".string_between_markers("START", "END")
#=> "foofoofoo"

#2


0  

Here's some alternate ways to do what you want, that are how I'd go about it:

这里有一些替代的方法来做你想做的事,这就是我要做的:

s = "Charges for the period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:<br>\nAny Network Cap remaining: $366.550<br>International Cap remaining: $0.000"  # => "Charges for the period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:<br>\nAny Network Cap remaining: $366.550<br>International Cap remaining: $0.000"

dt1, dt2 = /period (\S+ \S+) to (\S+ \S+):/.match(s).captures  # => ["2012-01-28 00:00:00", "2012-02-27 23:59:59"]
dt1                                                            # => "2012-01-28 00:00:00"
dt2                                                            # => "2012-02-27 23:59:59"

This is using "period" and "to" and the trailing ":" to mark the begin and end of the range to be searched for, and grabbing the non-whitespace characters that signify the date and time in each datetime stamp.

这是使用“句点”和“to”以及结尾的“:”来标记要搜索范围的开始和结束,并获取表示每个datetime戳记中的日期和时间的非空格字符。

Alternately, using "named-captures" predefines the variables:

或者,使用“命名捕获”预定义变量:

/period (?<dt1>\S+ \S+) to (?<dt2>\S+ \S+):/ =~ s  # => 16
dt1                                                # => "2012-01-28 00:00:00"
dt2                                                # => "2012-02-27 23:59:59"

From that point, if you want to break down the values returned you could parse them as dates:

从那时起,如果您想分解返回的值,您可以将它们解析为日期:

require 'date'
d1 = DateTime.strptime(dt1, '%Y-%m-%d %H:%M:%S')  # => #<DateTime: 2012-01-28T00:00:00+00:00 ((2455955j,0s,0n),+0s,2299161j)>
d1.month                                          # => 1
d1.day                                            # => 28

Or you could even use sub-captures:

或者你甚至可以使用子捕获:

matches = /period (?<dt1>(?<date1>\S+) (?<time1>\S+)) to (?<dt2>(?<date2>\S+) (?<time2>\S+)):/.match(s)
matches # => #<MatchData "period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:" dt1:"2012-01-28 00:00:00" date1:"2012-01-28" time1:"00:00:00" dt2:"2012-02-27 23:59:59" date2:"2012-02-27" time2:"23:59:59">
matches['dt1']   # => "2012-01-28 00:00:00"
matches['date1'] # => "2012-01-28"
matches['time2'] # => "23:59:59"

This is all documented in the Regexp documentation.

这在Regexp文档中都有记录。

#1


61  

input_string = "blahblahblahSTARTfoofoofooENDwowowowowo"
str1_markerstring = "START"
str2_markerstring = "END"

input_string[/#{str1_markerstring}(.*?)#{str2_markerstring}/m, 1]
#=> "foofoofoo"

or to put it in a method:

或者用一种方法:

class String
  def string_between_markers marker1, marker2
    self[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
  end
end

"blahblahblahSTARTfoofoofooENDwowowowowo".string_between_markers("START", "END")
#=> "foofoofoo"

#2


0  

Here's some alternate ways to do what you want, that are how I'd go about it:

这里有一些替代的方法来做你想做的事,这就是我要做的:

s = "Charges for the period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:<br>\nAny Network Cap remaining: $366.550<br>International Cap remaining: $0.000"  # => "Charges for the period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:<br>\nAny Network Cap remaining: $366.550<br>International Cap remaining: $0.000"

dt1, dt2 = /period (\S+ \S+) to (\S+ \S+):/.match(s).captures  # => ["2012-01-28 00:00:00", "2012-02-27 23:59:59"]
dt1                                                            # => "2012-01-28 00:00:00"
dt2                                                            # => "2012-02-27 23:59:59"

This is using "period" and "to" and the trailing ":" to mark the begin and end of the range to be searched for, and grabbing the non-whitespace characters that signify the date and time in each datetime stamp.

这是使用“句点”和“to”以及结尾的“:”来标记要搜索范围的开始和结束,并获取表示每个datetime戳记中的日期和时间的非空格字符。

Alternately, using "named-captures" predefines the variables:

或者,使用“命名捕获”预定义变量:

/period (?<dt1>\S+ \S+) to (?<dt2>\S+ \S+):/ =~ s  # => 16
dt1                                                # => "2012-01-28 00:00:00"
dt2                                                # => "2012-02-27 23:59:59"

From that point, if you want to break down the values returned you could parse them as dates:

从那时起,如果您想分解返回的值,您可以将它们解析为日期:

require 'date'
d1 = DateTime.strptime(dt1, '%Y-%m-%d %H:%M:%S')  # => #<DateTime: 2012-01-28T00:00:00+00:00 ((2455955j,0s,0n),+0s,2299161j)>
d1.month                                          # => 1
d1.day                                            # => 28

Or you could even use sub-captures:

或者你甚至可以使用子捕获:

matches = /period (?<dt1>(?<date1>\S+) (?<time1>\S+)) to (?<dt2>(?<date2>\S+) (?<time2>\S+)):/.match(s)
matches # => #<MatchData "period 2012-01-28 00:00:00 to 2012-02-27 23:59:59:" dt1:"2012-01-28 00:00:00" date1:"2012-01-28" time1:"00:00:00" dt2:"2012-02-27 23:59:59" date2:"2012-02-27" time2:"23:59:59">
matches['dt1']   # => "2012-01-28 00:00:00"
matches['date1'] # => "2012-01-28"
matches['time2'] # => "23:59:59"

This is all documented in the Regexp documentation.

这在Regexp文档中都有记录。