Using Ruby + regex, given:
starting-middle+31313131313@mysite.com
I want to obtain just: 31313131313
ie, what is between
Here is a solution based on regex lookbehind and lookahead.
email = "starting-middle+31313131313@mysite.com"
regex = /(?<=\+).*(?=@)/
regex.match(email)
=> #
Lookahead is indispensable if you want to match something followed by something else. In your case, it's a position followed by @, which express as (?=@)
Lookbehind has the same effect, but works backwards. It tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there. In your case, it's a position after +
, which express as (?<=\+)
so we can combine those two conditions together.
lookbehind (what you want) lookahead
↓ ↓ ↓
(?<=\+) .* (?=@)
Regex: Lookahead and Lookbehind Zero-Length Assertions