Solved

How can I make this regular expression case insensitive?

Posted on 2009-06-30
14
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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 …
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…
Suggested Courses

732 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