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

前端 未结 4 1158
我在风中等你
我在风中等你 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:01

    There's also Regexp#match, which returns a MatchData object, which has all the information you could possibly want.

    irb> match = /your username is: (.*)/.match "hello john\nyour username is: jj\nthanks for signing up\n"
    #=> #
    irb> match.pre_match
    #=> "hello john\n"
    irb> match.post_match
    #=> "\nthanks for signing up\n"
    irb> match[0]
    #=> "your username is: jj"
    irb> match[1]
    #=> "jj"
    

提交回复
热议问题