Solved

How can I make this regular expression case insensitive?

Posted on 2009-06-30
14
712 Views
Last Modified: 2012-05-07
The regular expression below evaluates email addresses in my RegularExpressionValidator.  It is case-sensitive and only allows lowercase.  I would like it to allow uppercase as well.  I don't even understand how the thing works really, so I don't know where to begin modifying it.  How do I make it case-insensitive?
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Open in new window

0
Comment
Question by:mrcoulson
  • 7
  • 7
14 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 24745705
use RegexOptions.IgnoreCase option.
0
 

Author Comment

by:mrcoulson
ID: 24745728
You may call me a lazy programmer, but this is all handled by a RegularExpressionValidator.
<asp:RegularExpressionValidator ID="vldSenderEmailFormat" runat="server" ControlToValidate="txtSenderEmail" ErrorMessage="Please enter a valid email address in the 'Your Email' field." ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" Display="None" />

Open in new window

0
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 24745758
You could also use:
[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?

or:
[\w!#$%&'*+/=?^`{|}~-]+(?:\.[\w!#$%&'*+/=?^`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?
0
 

Author Comment

by:mrcoulson
ID: 24745767
Lord, I need to read a book on regular expressions.  Let me try those!

Jeremy
0
 

Author Comment

by:mrcoulson
ID: 24745794
Both of those evaluate false.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24745800
Show me the test string.
0
 

Author Comment

by:mrcoulson
ID: 24745820
Wait a second.  It could be that I wasn't paying attention and pasted them into the wrong document.  Der.

Jeremy
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:mrcoulson
ID: 24745858
The first one seems to work.  Whew.  Could you go the extra mile and explain it a little to me?  Regular expressions look like massive sneezings of random characters and I'd like to understand them better.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24745980
I can only say that I added A-Z to the pattern in all places that you had a-z to make it case-insensitive.
I cannot say that the pattern actually will validate e-mail addresses (I would submit that validation of any/all e-mail addresses cannot be done with regex alone).

But, for some syntax starters:
[...] is a character set, any characters included within are treated as one set.
* is repeating 0 or more times.
+ is repeating 1 or more times.
. is any character
\ escapes any special characters such as . to make them match literally, such as \. would match "."
\w is a character class for [A-Za-z_]

I have a online regex tester here (PHP PCRE/.NET):
http://www.myregextester.com/

A cheatsheet:
http://regexlib.com/CheatSheet.aspx

Tutorials:
http://www.regular-expressions.info/
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24746730
mrcoulson, is there anything else I can provide regarding the solution?
0
 

Author Comment

by:mrcoulson
ID: 24746804
No, points are forthcoming.  I really appreciate the extra effort with the explanations.  I think I seriously need to do some studying in my downtime.

Thanks!

Jeremy
0
 

Author Closing Comment

by:mrcoulson
ID: 31598356
Thanks a ton!
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24746831
If you are serious about learning more you might consider finding a copy of the definitive reference text on the subject:
http://regex.info/
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 24746832
Thanks for the question and the points.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now