问个最基本的正则表达式 <title>aaaaa</title>想提取出aaaaa 里面这个aaaaa长度不固定 内容不固定 也就是.* 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 能把<title>aaaaa</title>取出来 <title id="ti" return="server">aaaaa</title>string str=ti.InnerText;int i=str.Lengthi就是字符串的长度 强(?<=<title>).*(?=</title) "(?si)<title>(?<title>.*?)</title>"捕获组 title 即为所求。 string pa = @"(?<=<title>).*(?=</title)"; Regex reg = new Regex(pa); Match m = reg.Match("<title>aaaaa</title>"); string s=m.Value; 表4.常用分组语法分类 代码/语法 说明捕获 (exp) 匹配exp,并捕获文本到自动命名的组里 (?<name>exp) 匹配exp,并捕获文本到名称为name的组里,也可以写成(?'name'exp) (?:exp) 匹配exp,不捕获匹配的文本,也不给此分组分配组号零宽断言 (?=exp) 匹配exp前面的位置 (?<=exp) 匹配exp后面的位置 (?!exp) 匹配后面跟的不是exp的位置 (?<!exp) 匹配前面不是exp的位置注释 (?#comment) 这种类型的分组不对正则表达式的处理产生任何影响,用于提供注释让人阅读 把空格去掉,空格是csdn给我加的 string pa = @"(?<=<title>).*(?=</title)"; Regex reg = new Regex(pa); Match m = reg.Match("<title>aaaaa</title>"); string s=m.Value; angel6709,能把你前面发的那个正则的文档给我一份吗 [email protected],先谢谢了 string input = @"<title>aaaaaa</title>";string pattern1 = "(?is)(?<=<title>)((?!</title>).)*";string pattern2 = "(?is)(?<=<title>).*?(?=</title>)";Match m = Regex.Match(input, pattern1);while(m.Success){ Console.WriteLine(m.Value); m = m.NextMatch();}MatchCollection mc = Regex.Matches(input, pattern2);foreach(Match m in mc){ Console.WriteLine(m.Value);} 线程自定义传参委托 按钮的颜色咋没变呢? 请问在程序中如何调用某个Button事件 关于SQL导出EXCEL的问题 多线程操作FORM控件的问题 C++到C# C#里如何像C++那样传递一个数组的地址 关于.net 中内在占用过大问题。 请问在winform中如何创建一个cookie? 语法问题!怎样理解这样一个类,就几行,但觉得很值得探讨~! 急问个DATAGIRD的问题 File.Exist() 判断远程文件为什么不稳定? 值方式传递和ref方式传递下面哪个参数修改后不鞥保留啊?
<title>aaaaa</title>
取出来
string str=ti.InnerText;
int i=str.Lengthi就是字符串的长度
(?<=<title>).*(?=</title)
捕获组 title 即为所求。
Regex reg = new Regex(pa);
Match m = reg.Match("<title>aaaaa</title>");
string s=m.Value;
分类 代码/语法 说明
捕获 (exp) 匹配exp,并捕获文本到自动命名的组里
(?<name>exp) 匹配exp,并捕获文本到名称为name的组里,也可以写成(?'name'exp)
(?:exp) 匹配exp,不捕获匹配的文本,也不给此分组分配组号
零宽断言 (?=exp) 匹配exp前面的位置
(?<=exp) 匹配exp后面的位置
(?!exp) 匹配后面跟的不是exp的位置
(?<!exp) 匹配前面不是exp的位置
注释 (?#comment) 这种类型的分组不对正则表达式的处理产生任何影响,用于提供注释让人阅读
string pa = @"(?<=<title>).*(?=</title)";
Regex reg = new Regex(pa);
Match m = reg.Match("<title>aaaaa</title>");
string s=m.Value;
string input = @"<title>aaa
aaa</title>";
string pattern1 = "(?is)(?<=<title>)((?!</title>).)*";
string pattern2 = "(?is)(?<=<title>).*?(?=</title>)";
Match m = Regex.Match(input, pattern1);
while(m.Success)
{
Console.WriteLine(m.Value);
m = m.NextMatch();
}
MatchCollection mc = Regex.Matches(input, pattern2);
foreach(Match m in mc)
{
Console.WriteLine(m.Value);
}