简体   繁体   中英

Find text lower case or upper case

I need find text lower case or upper case (using regex) My code:

static void Main(string[] args)
{
    String s = "String : hello Hello HELLO hEllo ";
    String patern = @"(hello)";
    Regex myRegex = new Regex(patern);
    foreach (Match regex in myRegex.Matches(s)) {
        Console.WriteLine(regex.Value.ToString());
    }
}

It result :

hello

I need result

hello 
Hello 
HELLO 
hEllo

Can you help me?

Two ways:

 String patern = @"(?i)(hello)";

(?i) turns on case-insensitive comparison, and (?-i) restores the default case-sensitive comparison.

Or use the RegexOptions.IgnoreCase option when creating your regex object:

Regex myRegex = new Regex(patern, RegexOptions.IgnoreCase);

尝试这个

String patern = @"[Hh][Ee][Ll][Ll][Oo]";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM