regex - Regular expression C# issue ( underscore) -


how can make regex let me put mandatory underscore "_" @ position of string of characters.

my initial regex is: ^(?=.*\d)(?=.*[a-za-z])[a-za-z0-9]{1,5}$ means: lower , upper case letters , decimal digits. i've tried escaping underscore this: \_

the conditions match are: mandatory: uppercase letters, lowercase letters, underscore "_" optional: numbers (decimal digits)

sample match strings:

_hola

h_o1a

ho_l3

h0l_a

hola_

public static bool trymatchwithregex(string txt, out string result) {     result = string.empty;     // mandatory= underscore, lowercase, uppercase     string pattern = @"^(?=.*_)(?=.*[a-z])(?=.*[a-z]).*$";     regex regex = new regex(pattern, regexoptions.none);     match match = regex.match(txt);     if (match.success)     {         result = match.value;         return true;     }     return false; } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -