Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case insensitive regex

Tags:

c#

.net

regex

I want to validate an email address with Regex in C#.

I'm using this pattern:

^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

This pattern only matches upper case letters. For example:

"[email protected]" --> returns false. "[email protected]" --> returns true.

I obviously would like that the first example will also return true.

NOTE: I DON'T want to use the RegexOptions.IgnoreCase flag.

I would like to change the pattern itself to match the first example. I think that I can add a "/i" in the end of the pattern or something like this but it doesn't seems to work. I prefer not to use the "?i" in the beginning as well.

How can i achieve this?

(If you could rewrite the whole pattern for me, it would be great!).

Thanks.

like image 477
ofirbt Avatar asked Jan 26 '11 08:01

ofirbt


People also ask

How do you make a case insensitive in regex?

(? i) makes the regex case insensitive.

Are regex expressions case-sensitive?

By default, the comparison of an input string with any literal characters in a regular expression pattern is case-sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly.

What does \+ mean in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

Does case matter in regex?

Discussion. Currently, REGEXP is not case sensitive, either.


1 Answers

Instead of just [A-Z], use [A-Za-z].

But watch out: there are e-mail addresses that end in top-level domains like .travel, that are forbidden according to your regex!

like image 177
Hans Kesting Avatar answered Oct 07 '22 08:10

Hans Kesting