我通过httprespone得到的一段网页代码:我通过httprespone得到的一段网页代码
现在想取出hash的值
用了以下表达式但是匹配不成功:Regex re = new Regex("hash=\\\"(\\w{40})\\\"");求教正确的表达式另外怎么样把“\u5b87\u5b99\u5927\u7206\u70b8”这种字符转换为中文输出?
现在想取出hash的值
用了以下表达式但是匹配不成功:Regex re = new Regex("hash=\\\"(\\w{40})\\\"");求教正确的表达式另外怎么样把“\u5b87\u5b99\u5927\u7206\u70b8”这种字符转换为中文输出?
解决方案 »
- Trace和Debug的使用
- 参数传递和变量复制之间的关系
- 一个list<string>,或者vector<string> 有什么好的方法去重
- C# 线程的运用
- DataGrid的问题
- 网络编程问题
- ASP.net(C#)如何向数据库里插入时间?
- rational xde 2003 for .net 带 license 求交换 borland together 1.01 for .net 和 license
- 在.cs文件中调用javascript,在传递参数时有误,请问怎样获取到此参数据?
- 向高手请教:关于用C#来做系统自动备份的问题! 分不够再加!
- 得到打开默认程序的文件的路径?
- 根据两点,获取贝塞尔曲线.
Regex reg = new Regex(@"hash=""([^'""\s]+)""");
Response.Write(reg.Match(str).Groups[1].Value);
Response.Write(HttpUtility.UrlDecode("\u5b87\u5b99\u5927\u7206\u70b8",Encoding.UTF8));
/*
81d4f3ebe4cefaad4a304f9bd845958c71bfe594
宇宙大爆炸
*/
那个字体转换应该是
Regex.Unespase()
string str="hash=\"81d4f3ebe4cefaad4a304f9bd845958c71bfe594\" filesize=\"4531141884\" cmd_len=\"1\" filename=\"[CSDHD]\u5b87\u5b99\u5927\u7206\u70b8The.Big.Bang.2011.BluRay.720p.wmv\"";
string pattern = @"(?<=hash="")\w{40}";
Regex regex = new Regex(pattern);
Console.WriteLine(regex.Match(str).Value);
//result:81d4f3ebe4cefaad4a304f9bd845958c71bfe594
用这个试试呢string str="hash=\\\"81d4f3ebe4cefaad4a304f9bd845958c71bfe594\\\""
你在变量里面实际上已经把“\"”转义成“"”了
而我得到的原始字符串就是“hash=\"81d4f3ebe4cefaad4a304f9bd845958c71bfe594\"”才对
试试先解码再正则呢:string str ="....."//原始字符串
string html = Regex.Unespase(str);
Regex re = new Regex(@"\bhash=""(\w{40})""");
/*
处理过程
*/貌似能够匹配成功
Regex reg = new Regex(@"hash=\\""([^'""\s]+)\\""");
Response.Write(reg.Match(str).Groups[1].Value);/*
TextFile.txt
hash=\"81d4f3ebe4cefaad4a304f9bd845958c71bfe594\" filesize=\"4531141884\" cmd_len=\"1\" filename=\"[CSDHD]\u5b87\u5b99\u5927\u7206\u70b8The.Big.Bang.2011.BluRay.720p.wmv\"
*/
请恕小弟啰嗦!
Regex reg = new Regex(@"hash=\\""([^""]+)\\""(?:(?!filename).)*filename=\\""([^""]+)\\""");
Response.Write(reg.Match(str).Groups[1].Value + "===" + reg.Match(str).Groups[2].Value);
/*
81d4f3ebe4cefaad4a304f9bd845958c71bfe594===[CSDHD]\u5b87\u5b99\u5927\u7206\u70b8The.Big.Bang.2011.BluRay.720p.wmv
*/