正则表达式以提取此HTML文件的一部分

时间:2012-06-07 23:37:32

标签: c# html regex

我有一个包含此脚本的html页面:

flashembed("header_container", 
{"src": "http://****.swf?__cv=3cfd4cc0ac1fad53803ff73629e93d00",
"version": [8,0],
"expressInstall": "http://****.swf?__cv=87411ea96ce42429f52b28683e7af400",
"width": 860,"height": 229,"wmode": "opaque","id": "flashHeader",
"onFail": 
function(){onFailFlashembed();}}, 
{"cdn": "http://*****/","nosid": "1","lol": "89,04","isGuestUser": "",
"navPoint": "1","eventItemEnabled": "",
"supporturl":"indexInternal.es%3Faction%3Dsupport%26back%3DinternalStart",




"***ouser***": "817",



"serverdesc": "Italia 3","server_code": "1","lang": "it","coBrandImgUrl": "",
"coBrandHref": "","customSkinURL": "","messaging": "1"});

hackEmailInviteDialog();
jQuery('#emailInviteCloseButton').click(function() 
{
.....
}

我需要提取字段" ouser"从这个页面。我尝试过:

string pattern= @"""ouser"": "".*?,""serverdesc""";
string output = Regex.Replace(ConnectionAPI.responseFromServer, pattern, ""); 

但是在输出中有整个页面......

2 个答案:

答案 0 :(得分:3)

更新正则表达式以匹配第二对引号之间的任何内容,以防它们不总是数字。

Match match = 
    Regex.Match(
        ConnectionAPI.responseFromServer, 
        "\"\\**?ouser\\**?":\\s*\"([^\"]*)\",",
        RegexOptions.IgnoreCase);
String output = String.Empty;
// Here we check the Match instance.
if (match.Success)
{
    // Finally, we get the Group value and display it.
    output = match.Groups[1].Value;
    Console.WriteLine(output);
}

答案 1 :(得分:2)

"\**?ouser\**?":\s*"(\d\w+)

第1组与该文档中的817匹配。 Play with the regex here

虽然如果你在任意标签上进行大量的HTML解析,你可以使用SAXDOM解析器做得更好。 Andrew Finnell还提到了使用JSONWebKit

正如merlin2011所提到的那样,Regex.Replace替换您要提取的内容,而不是为您抓取它。