Ruby Regexp group matching, assign variables on 1 line

前端 未结 5 834
旧巷少年郎
旧巷少年郎 2020-12-07 12:38

I\'m currently trying to rexp a string into multiple variables. Example string:

ryan_string = \"RyanOnRails: This is a test\"

I\'ve mat

5条回答
  •  醉梦人生
    2020-12-07 13:21

    You could use Match or =~ instead which would give you a single match and you could either access the match data the same way or just use the special match variables $1, $2, $3

    Something like:

    if ryan_string =~ /(^.*)(:)(.*)/i
       first = $1
       third = $3
    end
    

提交回复
热议问题