1

我需要查找文本小写或大写(使用正则表达式)我的代码:

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());
    }
}

结果:

hello

我需要结果

hello 
Hello 
HELLO 
hEllo

你能帮助我吗?

4

3 回答 3

7

两种方式:

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

(?i) 打开不区分大小写的比较, (?-i) 恢复默认的区分大小写比较。

或者RegexOptions.IgnoreCase在创建正则表达式对象时使用该选项:

Regex myRegex = new Regex(patern, RegexOptions.IgnoreCase);
于 2013-06-26T02:53:58.907 回答
0

尝试这个

String patern = @"[Hh][Ee][Ll][Ll][Oo]";
于 2013-06-26T02:50:26.083 回答
0

搜索时传递 RegexOptions.IgnoreCase。

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx

于 2013-06-26T02:53:57.550 回答