Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex get domain name from email

Tags:

I am learning regex and am having trouble getting google from email address

String

[email protected] 

I just want to get google, not google.com

Regex:

[^@].+(?=\.) 

Result: https://regex101.com/r/wA5eX5/1

From my understanding. It ignore @ find a string after that until . (dot) using (?=\.)

What did I do wrong?

like image 827
I'll-Be-Back Avatar asked Aug 18 '16 20:08

I'll-Be-Back


1 Answers

[^@] means "match one symbol that is not an @ sign. That is not what you are looking for - use lookbehind (?<=@) for @ and your (?=\.) lookahead for \. to extract server name in the middle:

(?<=@)[^.]+(?=\.) 

The middle portion [^.]+ means "one or more non-dot characters".

Demo.

like image 96
Sergey Kalinichenko Avatar answered Sep 21 '22 19:09

Sergey Kalinichenko