急?正则取值取不到!

wsjrzjp 2012-05-22 11:44:39
string RegexString = "<title>.+?</title>";
string pageStr = "2323232323<title>Salyani Technologies (P) Ltd.</title>23232323232";
string resString = "";
Regex reg = new Regex(RegexString, RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(pageStr);
foreach (Match match in matches)
{
resString += match.Groups[1].Value;
}
Response.Write(resString+"/Test");

...全文
75 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kunkun0921 2012-05-22
  • 打赏
  • 举报
回复

string RegexString = ".*?<title>(.+?)</title>.*?";
string pageStr = "2323232323<title>Salyani Technologies (P) Ltd.</title>23232323232";
string resString = "";
Regex reg = new Regex(RegexString, RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(pageStr);
foreach (Match match in matches)
{
resString += match.Groups[1].Value;
}

kunkun0921 2012-05-22
  • 打赏
  • 举报
回复
string RegexString = ".*?<title>.+?</title>.*?";
devmiao 2012-05-22
  • 打赏
  • 举报
回复
RegexString = "<title>.+?</title>";
=>
RegexString = "<title>(.+?)</title>";
wsjrzjp 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
一种这样

C# code

string RegexString = "<title>(.+?)</title>";
string pageStr = "2323232323<title>Salyani Technologies (P) Ltd.</title>23232323232";
string resString = "";
Regex reg = new ……
[/Quote]

非常感谢
EnForGrass 2012-05-22
  • 打赏
  • 举报
回复
一种这样

string RegexString = "<title>(.+?)</title>";
string pageStr = "2323232323<title>Salyani Technologies (P) Ltd.</title>23232323232";
string resString = "";
Regex reg = new Regex(RegexString, RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(pageStr);
foreach (Match match in matches)
{
resString += match.Groups[1].Value;
}
Response.Write(resString+"/Test");

另一种这样

string RegexString = "(?<=<title>).+?(?=</title>)";
string pageStr = "2323232323<title>Salyani Technologies (P) Ltd.</title>23232323232";
string resString = "";
Regex reg = new Regex(RegexString, RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(pageStr);
foreach (Match match in matches)
{
resString += match.Value;
}
Response.Write(resString+"/Test");

以上两种都行,取出的值是Salyani Technologies (P) Ltd.

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧