Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby, using regex to find something in between two strings

Tags:

regex

ruby

Using Ruby + regex, given:

[email protected]

I want to obtain just: 31313131313

ie, what is between starting-middle+ and mysite.com

Here's what I have so far:

to = '[email protected]'

to.split(/\+/@mysite.com.*/).first.strip
like image 723
AnApprentice Avatar asked Nov 18 '10 20:11

AnApprentice


2 Answers

Between 1st + and 1st @:

to[/\+(.*?)@/,1]

Between 1st + and last @:

to[/\+(.*)@/,1]

Between last + and last @:

to[/.*\+(.*)@/,1]

Between last + and 1st @:

to[/.*\+(.*?)@/,1]
like image 126
Nakilon Avatar answered Oct 03 '22 13:10

Nakilon


Here is a solution without regex (much easier for me to read):

i = to.index("+")
j = to.index("@")
to[i+1..j-1]
like image 38
Powers Avatar answered Oct 03 '22 11:10

Powers