Regular expression without registers

I want to check email address with Regex in C #.

I am using this template:

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

This pattern matches only lowercase letters. For instance:

" example@gmail.com " β†’ returns false. " EXAMPLE@GMAIL.COM " β†’ returns true.

I obviously would like the first example to return true as well.

NOTE. I DO NOT want to use the RegexOptions.IgnoreCase flag.

I would like to modify the template itself to fit the first example. I think I can add "/ i" at the end of the pattern or something like this, but it does not seem to work. I prefer not to use "? I" at the beginning.

How can i achieve this?

(If you could rewrite the entire template for me, that would be great!).

Thanks.

+4
source share
3 answers

Instead of [AZ] use [A-Za-z].

But be careful: there are email addresses that end in top-level domains, such as .travel, which are forbidden according to your regular expression!

+14
source

You can simply use: ^(?i)[A-Z0-9._%-] +@ [A-Z0-9.-]+\.[AZ]{2,4}$

Note the (?i) that sets RegexOptions.IgnoreCase , so you don’t have to change the character classes in the regular expression or change where the code is used.

Example (in F # interactive):

 Regex.Match(" test@test.com ", @"^(?i)[A-Z0-9._%-] +@ [A-Z0-9.-]+\.[AZ]{2,4}$");; val it : Match = test@test.com {Captures = seq [...]; Groups = seq [...]; Index = 0; Length = 13; Success = true; Value = " test@test.com ";} 
+10
source

Well, that will work.

 ^[A-Za-z0-9._%-] +@ [A-Za-z0-9.-]+.[A-Za-z]{2,4}$ 
+3
source

Source: https://habr.com/ru/post/1337108/


All Articles