473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Regular expression question

/^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/

I'm using this to try to validate a small subset of the valid e-mail
addresses allowed by the relevant RFC (alphanumerics, underscores, and
dashes). I've tested it and it seems to work - I'm just looking for
helpful hints :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
6 1062
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
/^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/

I'm using this to try to validate a small subset of the valid e-mail
addresses allowed by the relevant RFC (alphanumerics, underscores, and
dashes). I've tested it and it seems to work - I'm just looking for
helpful hints :)


Well, one hint would be to not take such a restrictive subset. :)

The range [a-zA-Z0-9_] can be written as \w, and [.] and \. are
equivalent, so a shorter version is:

/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/

(I changed the last "*" to "+", because all domain names must have at
least one ".").

Again, I can't see what real purpose such a restrictive subset can
be used for. :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
Christopher Benson-Manica wrote:
/^[a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*@[a-zA-Z_-]+([.][a-zA-Z0-9_-]+)*$/


Rather than the character class

[a-zA-Z0-9_-]

you can use

[\w-]

I'd also escape the dots with a backslash, rather than specifying a
character class.

Making the last group optional is debatable. Whilst I believe it's
valid (an address could be a single string if it aliases another
destination), it's not of much value on the Web as all domains will
have at least one dot.

Out of interest, did you intend to make the two character classes
after the @ different, or is one of them a typo?

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter <m.******@blueyonder.co.invalid> spoke thus:
[\w-]
I wasn't aware you could do that - I will. Thanks.
Out of interest, did you intend to make the two character classes
after the @ different, or is one of them a typo?


It was a typo, which I suppose further argues in favor of the
abbreviated version you presented above :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #4
Lasse Reichstein Nielsen <lr*@hotpop.com> spoke thus:
Again, I can't see what real purpose such a restrictive subset can
be used for. :)


Do any real e-mail addresses use the other allowable characters? We
just wanted something fairly simple without characters that might
cause problems elsewhere.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #5
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Do any real e-mail addresses use the other allowable characters? We
just wanted something fairly simple without characters that might
cause problems elsewhere.


I regularly *try* to enter something on the form
na************@example.com
That allows me to quickly create a unique address for each recipient,
so I can see who gives my addres to spammers (and allows me to block it),
all using only one mailbox.

However "cause problems elsewhere" is so vague, that perhaps you
should decide what programs the address must interact with, and what
their requirements are. After all, you probably don't want to turn
somebody away, just because their e-mail address is not matching
the lowest common denominator, unless you *really* need to.

(but no, I don't usually see such addresses)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
Lasse Reichstein Nielsen <lr*@hotpop.com> spoke thus:
I regularly *try* to enter something on the form
na************@example.com
That allows me to quickly create a unique address for each recipient,
so I can see who gives my addres to spammers (and allows me to block it),
all using only one mailbox.
Hm, I see. I'm fairly certain that we're not a spam entity though :)
However "cause problems elsewhere" is so vague, that perhaps you
should decide what programs the address must interact with, and what
their requirements are. After all, you probably don't want to turn
somebody away, just because their e-mail address is not matching
the lowest common denominator, unless you *really* need to.


Well, I suppose if we get complaints, we can URIEncode the form fields
and allow some more of the characters, but that doesn't sound too
likely.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
5
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.