Extract a substring from a string in Ruby using a regular expression

后端 未结 5 1828
栀梦
栀梦 2020-11-30 19:05

How can I extract a substring from within a string in Ruby?

Example:

String1 = \" \"

I want to extract

5条回答
  •  再見小時候
    2020-11-30 19:36

    " "[/.*<([^>]*)/,1]
    => "substring"
    

    No need to use scan, if we need only one result.
    No need to use Python's match, when we have Ruby's String[regexp,#].

    See: http://ruby-doc.org/core/String.html#method-i-5B-5D

    Note: str[regexp, capture] → new_str or nil

提交回复
热议问题