2

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.

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

8 Comments

@GrantThomas learn a programming language, and you should know
What about when learning a language? What about when the languages are mixed?
@GrantThomas please, write an answer that explains everything, i will upvote it. but i wont explain stuff that anyone (even when learning to program) can find out in a simple google search, probably very well explained
Well that's your privilege, but doesn't exactly make a remarkable answer.
@GrantThomas im not trying to be remarkable here, but is it so hard to enter 'escape special characters regex' into a search engine?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.