I want to get string from a string with 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:
c&&(b.signature=hj(c));
And i want to get the hj
, and when i run it i get this 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
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))
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.