Solved

How can I make this regular expression case insensitive?

Posted on 2009-06-30
14
715 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…

830 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