How to return the substring of a string between two strings in Ruby?

后端 未结 3 1929
梦如初夏
梦如初夏 2020-12-08 04:22

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

For example I have:

  • input_string
  • str1_marker
相关标签:
3条回答
  • 2020-12-08 04:29
    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"
    
    0 讨论(0)
  • 2020-12-08 04:33

    Just split it twice and get the string between the markers:

    input_string.split("str1_markerstring").last.split("str2_markerstring").first
    
    0 讨论(0)
  • 2020-12-08 04:41

    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.

    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.

    0 讨论(0)
提交回复
热议问题