If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQL REGEX

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-09, 09:13
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
MySQL REGEX

Hi

does anybody know what the regular expression would be to match the domain part of an email address?

e.g

I have database column with email addresses. I would like to select all emails that have the same domain part i.e @dbforums.com

i use the following

Code:
SELECT SUBSTRING_INDEX(inEmail, '@', -1)
	 INTO emailDomain;
	  
  SELECT email
  	FROM tbl_employees
   WHERE email REGEXP CONCAT('@', emailDomain, '$')
is there anyway i can do this by just using a REGEXP without having to do the substring bit to retrieve the domain part?
Reply With Quote
  #2 (permalink)  
Old 07-26-09, 09:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the problem here is that you need to specify which domain you're after

because what you posted will return all emails in the table, and therefore you don't really need to "do the substring bit to retrieve the domain part"

if you're after the emails for a specific domain, just do this:
Code:
 WHERE email LIKE '%@example.com'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-26-09, 11:10
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by r937
the problem here is that you need to specify which domain you're after

because what you posted will return all emails in the table, and therefore you don't really need to "do the substring bit to retrieve the domain part"

if you're after the emails for a specific domain, just do this:
Code:
 WHERE email LIKE '%@example.com'

with the above I would still need to extract the domain part first before i can use the LIKE clause which doesn't solve the problem. What i want to do is use the full email address and the regular expression to match just the domain part. This would save me from doing the substring bit to retrieve the domain part to search on.
Reply With Quote
  #4 (permalink)  
Old 07-26-09, 14:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
which domain are you searching for?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-26-09, 15:54
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
it can be any domain. For example a company employee wants to register a user account. The employee registers using their email address and a password. Before an account is created I want to check the database to see if there are other employees registered with the same email domain.
Reply With Quote
  #6 (permalink)  
Old 07-26-09, 17:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by ozzii
it can be any domain. For example a company employee wants to register a user account. The employee registers using their email address and a password. Before an account is created I want to check the database to see if there are other employees registered with the same email domain.
but the point is, you are searching for a specific domain, yes?

then all you need is a WHERE cluase like in post #2

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-27-09, 08:25
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Couldn't you just run the following SQL and see if it returns a name. If it does then an account with the same email domain already exists. If it doesn't then it's a new domain:
Code:
select email as existing_domain
from   tbl_employees
where  SUBSTRING_INDEX(email, '@', -1) = SUBSTRING_INDEX("new_name@gs.com", '@', -1)
limit  1;
Obviously the "new_name@gs.com" will be a variable holding the email of the new account.

Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On