Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help modifying Regular Expression

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
I'm trying to modify a regular expression for an email address. It has recently come to my attention that the + is an acceptable character in the first part of an email address. Here's the expression as is:

^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$

I'd like to insert before the @ an optional +, but if you use plus then there should be more 1 or more characters after the plus. How do I do this?
 
Code:
^[!]([a-zA-Z0-9_\.\-\+])*[/!]([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
This works pretty good except for:

+my@emailaddress.com
 
maybe I'm misunderstanding your question, but running this provides the output "valid email", and seems to fit the rules you originally posted in your first question:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var re = /^([a-zA-Z0-9_\.\-\+])*([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var str = "+my@emailaddress.com";

if (re.test(str)) {
   alert("valid email");
}
else {
   alert("invalid email");
}

</script>

<style type="text/css">
</style>
</head>
<body>
</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
It does produce a "valid email" result, but what I specified was that the "+" be optional, however the there must be something in front of it. something like:

[required][if [+] then [at least 1 more char, otherwise BAD, NO MATCH] else [continue as normal]]@[required].[required]

does that make sense?
 
your last post said:
but what I specified was that the "+" be optional, [!]however the there must be something in front of it[/!]

Sorry, but that's not what you originally specified at all....

your first post said:
I'd like to insert before the @ an optional +, but if you use plus [!]then there should be more 1 or more characters after the plus[/!]. How do I do this?

You said there had to be at least 1 character after the plus, you never said anything about a character before the plus.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
My mistake. I would like there to be required text, an optional plus, and required text after the plus, then @ symbol, required text, . (dot), and finally required tld.

Thanks for the help.
 
Well, then I would use the block you have specified in your first post to signify the allowable characters before the +

You want to have at least one instance of a character before the plus, so you would follow that block with a + character - which in regexp means match 1 or more instances of the preceding character. Then you'll want to match a plus character - to do this you have to put a \ in front of the plus so that it doesn't evaluate as the "match 1 or more" character explained above. You haven't stated if you can have multiple + characters in a row, or if there has to be a non-plus character between each one. If there has to be exactly 1 + character, you would follow it with {1} - which means match exactly 1 of the previous character. If you can have multiple plus characters in a row, then you would want to follow that with another + which I explained above means match 1 or more. Now, take that whole big mess and wrap it in some parentheses. Then, follow the parentheses with * character. This means match 0 or more of the preceding character (or in our case 0 or more of the stuff inside the parentheses). Once you've got that all written, you can plop it directly in front of the intro character block you have in your original regexp (the stuff before the @), and that should work.

For example, if you use the {1} after the \+, it will match email addresses like these:

abc+def@blah.com
abc+def+ghi@blah.com
a+b+c+d+e+f+g+h+i@blah.com

And, if you use the + after the \+, it will match email addresses like these:

abc+def@blah.com
abc+def+ghi@blah.com
a+b+c+d+e+f+g+h+i@blah.com
abc++++++def@blah.com
abc+++def++++ghi@blah.com
etc.......


Anyway, that's the steps I would take to write it. If you have any problems post back what [!]you[/!] have written so far instead of an email regexp you downloaded off the internet and expected someone to modify for you.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top