Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex to extract username from email address

My string of text looks like this:

[email protected] (John Doe) 

I need to get just the part before the @ and nothing else. The text is coming from a simple XML object if that matters any.

The code I have looks like this:

$authorpre = $key->{"author"}; $re1 = '((?:[a-z][a-z]+))';  if ($c = preg_match_all ("/".$re1."/is", $authorpre, $matches)) {     $author = $matches[1][0]; } 

Sometimes the username might have numbers or an underscore before the @ symbol, which is where the regex stops it seems.

like image 639
mrpatg Avatar asked Nov 25 '09 16:11

mrpatg


People also ask

How do I extract names from email addresses in sheets?

In Google Sheets, I use this formula .. =REGEXREPLACE(C2,"(. *)@","") ... and it works perfectly.

How do I extract an email from a string?

Example of JavaScript extract email from string_-]+@[a-zA-Z0-9. _-]+\. [a-zA-Z0-9_-]+)/ to extract email ids (address) from the long text.


1 Answers

The regular expression that will match and capture any character until it reaches the @ character:

([^@]+) 

That seems like what you need. It'll handle all kinds of freaky variations on e-mail addresses.


I'm not sure why Ben James deleted his answer, since I feel it's better than mine. I'm going to post it here (unless he undeletes his answer):

Why use regex instead of string functions?

$parts = explode("@", "[email protected]"); $username = $parts[0]; 

You don't need regular expressions in this situation at all. I think using explode is a much better option, personally.


As Johannes Rössel points out in the comments, e-mail address parsing is rather complicated. If you want to be 100% sure that you will be able to handle any technically-valid e-mail address, you're going to have to write a routine that will handle quoting properly, because both solutions listed in my answer will choke on addresses like "a@b"@example.com. There may be a library that handles this kind of parsing for you, but I am unaware of it.

like image 52
Welbog Avatar answered Oct 03 '22 04:10

Welbog