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

后端 未结 5 1833
栀梦
栀梦 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:26

    String1.scan(/<([^>]*)>/).last.first
    

    scan creates an array which, for each in String1 contains the text between the < and the > in a one-element array (because when used with a regex containing capturing groups, scan creates an array containing the captures for each match). last gives you the last of those arrays and first then gives you the string in it.

提交回复
热议问题