[英]Parse String from string with Regex
I want to get string from a string with Regex
: 我想从正则Regex
的字符串中获取字符串:
Regex regex = new Regex(".signature=(.*)(", RegexOptions.Singleline);
var v = regex.Match(html);
string funcName = v.Groups[1].Value;
This is the a HTML
string: 这是一个HTML
字符串:
c&&(b.signature=hj(c));
And i want to get the hj
, and when i run it i get this exception
: 我想获得hj
,当我运行它时,出现此exception
:
parsing ".signature=(.*)(" - Not enough )'s.
you have to escape special characters. 您必须转义特殊字符。 use this: 用这个:
Regex regex = new Regex(@"\.signature=(.*)\(", RegexOptions.Singleline);
var v = regex.Match(html);
string funcName = v.Result("$1");
you can find a very good explanation about escaping special characters in regex here (2nd paragraph): http://www.regular-expressions.info/characters.html 您可以在正则表达式中找到有关转义特殊字符的很好的解释(第二段): http : //www.regular-expressions.info/characters.html
Edit: 编辑:
if you search for this specific function in an entire html page, you will have problems, that is because .*
is greedy, which means it tries to get as much as possbile (see a good explanation about that here: http://www.regular-expressions.info/repeat.html (3rd paragraph)) 如果您在整个html页面中搜索此特定功能,则会遇到问题,这是因为.*
贪婪,这意味着它尝试获取尽可能多的可能性(有关此内容的详细说明,请参见http:// www .regular-expressions.info / repeat.html (第3段)
a better way would be: 更好的方法是:
Regex regex = new Regex(@"\.signature=([^\(]+)\(", RegexOptions.Singleline);
var v = regex.Match(html);
string funcName = v.Result("$1");
[^\\(]+
searches for a string of at least 1 character without a (
. that would work on an entire html page [^\\(]+
搜索至少包含1个字符的字符串,且不包含(
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.