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

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

    You can use a regular expression for that pretty easily…

    Allowing spaces around the word (but not keeping them):

    str.match(/< ?([^>]+) ?>\Z/)[1]
    

    Or without the spaces allowed:

    str.match(/<([^>]+)>\Z/)[1]
    

提交回复
热议问题