gpt4 book ai didi

c# - 查找文本小写或大写

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:32 27 4
gpt4 key购买 nike

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

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

你能帮帮我吗?

最佳答案

两种方式:

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

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

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

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

关于c# - 查找文本小写或大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com