+ 2

Email address subtraction

Can I use "([\w\.-]+)@([\w\.-]+)“ instead of "([\w\.-]+)@([\w\.-]+)(\.[\w\.]+)“ I tried it, it seems to work well.

6th Mar 2018, 2:21 PM
rightdragon
rightdragon - avatar
7 Respostas
+ 14
The last part is for capturing the "." (dot) Otherwise hello@world is valid.
6th Mar 2018, 2:54 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 6
Like Swapnil already mentioned, that last bit captures the TLD (top-level domain). Mind though, that some addresses may contain 2- or 3-level subdomains (or perhaps even more), so that last regex group has to check for one or more occurences.
6th Mar 2018, 7:22 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
I think you can.
6th Mar 2018, 2:25 PM
Tiger Jones
Tiger Jones - avatar
+ 1
Thank you for replying. So I do not understand why this tutorial explain "([\w\.-]+)@([\w\.-]+)(\.[\w\.]+)“.
6th Mar 2018, 2:29 PM
rightdragon
rightdragon - avatar
+ 1
It explains this regex, because this captures the ending (.com) in a single capture group. Your version also captures text like a@b
6th Mar 2018, 2:33 PM
Tiger Jones
Tiger Jones - avatar
+ 1
Thanks for the detail. I got it. a@b is enough to grab email address, I think.
6th Mar 2018, 2:36 PM
rightdragon
rightdragon - avatar
+ 1
I see. Thank you very much, Swapnil and Kuba! I just begun to learn programme, pythin is my first programming lunguage, so I cannot image examples. Now, I understand this question clearly. Thanks again :).
7th Mar 2018, 1:13 AM
rightdragon
rightdragon - avatar