您的位置:首页 > 其它

正则表达式 匹配标签里面的值 eg:image input

2009-09-03 15:47 225 查看
Code
//得到input的values
public ArrayList GetInput(string htmlText)
{
Regex regex = new Regex("<title>(?<content>.*?)function JasonSearch()", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
string str = regex.Match(htmlText).Groups["content"].Value;

const string pattern = "<input [^~]*?>";
const string pattern1 = "value\\s*=\\s*((\"|\')?)(?<url>\\S+)(\"|\')?[^>]*";
ArrayList al = new ArrayList();
Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase);
while (match.Success)
{
string img = match.Value;
string imgsrc = Regex.Match(img, pattern1, RegexOptions.IgnoreCase).Result("${url}");
imgsrc = Regex.Replace(imgsrc, "\"|\'|\\>", "", RegexOptions.IgnoreCase);
al.Add(imgsrc);
match = match.NextMatch();

}
return al;

}

//得到图片的image路径
public static ArrayList GetImages(string htmlText)
{
const string pattern = "<img [^~]*?>";
const string pattern1 = "src\\s*=\\s*((\"|\')?)(?<url>\\S+)(\"|\')?[^>]*";
ArrayList al = new ArrayList();
Match match = Regex.Match(htmlText, pattern, RegexOptions.IgnoreCase); //找到img标记
while (match.Success)
{
string img = match.Value;
string imgsrc = Regex.Match(img, pattern1, RegexOptions.IgnoreCase).Result("${url}");
imgsrc = Regex.Replace(imgsrc, "\"|\'|\\>", "", RegexOptions.IgnoreCase);
al.Add(imgsrc);
match = match.NextMatch();

}
return al;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: