c# 贪婪匹配 html,C# 正则表达式贪婪模式案例(示例代码)

本文档详细介绍了如何使用正则表达式在C#中进行字符串匹配,包括静态方法Regex.Replace和RegexOptions.IgnoreCase选项的应用,以及Match和CaptureCollection对象的使用示例。通过实例展示了如何查找并替换特定模式,以及获取匹配项的详细信息。
摘要由CSDN通过智能技术生成

///

///去掉

static voidTest2()

{//要匹配的字符串

string text = @"hello

alert(‘1‘);

regex

alert(‘2‘);

world";//正则表达式

string pattern = @"";//string result = Regex.Replace(text, pattern, "").Replace("\r\n","").Replace(" ","");

string result = Regex.Replace(text, pattern, "").Replace(" ", "");

Console.WriteLine(result);

}///

///获取

static voidTest3()

{//要匹配的字符串

string text = @"hello

alert(‘1‘);

regex

alert(‘2‘);

world";//正则表达式

string pattern = @"";

Regex r= newRegex(pattern, RegexOptions.IgnoreCase);//string result = Regex.Replace(text, pattern, "").Replace("\r\n","").Replace(" ","");

MatchCollection matchCollection =r.Matches(text);foreach (Match m inmatchCollection)

{//显示匹配开始处的索引值和匹配到的值

System.Console.WriteLine("Match=[" + m + "]");

CaptureCollection cc=m.Captures;foreach (Capture c incc)

{

Console.WriteLine("/tCapture=[" + c + "]");

}for (int i = 0; i < m.Groups.Count; i++)

{

Group group=m.Groups[i];

System.Console.WriteLine("/t/tGroups[{0}]=[{1}]", i, group);for (int j = 0; j < group.Captures.Count; j++)

{

Capture capture=group.Captures[j];

Console.WriteLine("/t/t/tCaptures[{0}]=[{1}]", j, capture);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值