What's the “ruby way” to parse a string for a single key/value?

前端 未结 4 1168
我在风中等你
我在风中等你 2020-12-31 10:49

I am trying to parse a multi line string and get the rest of the line following a pattern.

text:

hello john
your username is: jj
thanks for signing up

4条回答
  •  不知归路
    2020-12-31 11:12

    Your code is pretty much the Ruby way. If you don't want to use the global $1, you can use the 2 arg version String#[]:

    match = text[/your username is: (.*)/, 1]
    

提交回复
热议问题