ruby, using regex to find something in between two strings

后端 未结 4 511
醉话见心
醉话见心 2021-02-04 06:37

Using Ruby + regex, given:

starting-middle+31313131313@mysite.com

I want to obtain just: 31313131313

ie, what is between

4条回答
  •  自闭症患者
    2021-02-04 07:12

    If you care about readability, i suggest to just use "split", like so: string.split("from").last.split("to").first or, in your case:

    to.split("+").last.split("@").first

    use the limit 2 if there are more occurancies of '+' or '@' to only care about the first occurancy: to.split("+",2).last.split("@",2).first

提交回复
热议问题