[英]Find text lower case or upper case
我需要查找文本的小寫或大寫(使用正則表達式)我的代碼:
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);
嘗試這個
String patern = @"[Hh][Ee][Ll][Ll][Oo]";
搜索時傳遞RegexOptions.IgnoreCase。
http://msdn.microsoft.com/zh-CN/library/system.text.regularexpressions.regexoptions.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.