Solved

How can I make this regular expression case insensitive?

Posted on 2009-06-30
14
713 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now