Reputation: 27
I'm trying to make this regex a string literal, and although i can get it to match in an online evaluator can't quite get it in visual studio. I know its a problem with my escape characters, any help would be appreciated.
string pattern = " ^getelementbyid\("(.*?)\" "
Trying to make this a string literal, valid regex in regex evaluator (regex101.com)
Regex regex = new Regex(pattern);
Upvotes: 0
Views: 1457
Reputation: 11356
I believe this is what you're looking for:
string pattern = @"^getelementbyid\(""(.*?)""";
P.S. - it looks like you're parsing JavaScript. Since strings in JavaScript can be contained in either double-quotes or single-quotes, you can make your regex more robust like so:
string pattern = @"^getelementbyid\(['""](.*?)['""]";
That will match getelementbyid("myID"
as well as getelementbyid('myID'
.
Upvotes: 1
Reputation: 1831
Not entirely sure what you are trying to match against, but based on what it appears you are trying to do, I think you may have 2 issues.
1) As Joe pointed pointed out, you are missing the escape on the first quote after getelementbyid.
2) You are also missing an escape before your first backslash after getelementbyid.
Since you didn't give us an example of what you are trying to validate, this is all just speculation though.
See if the following will provide you with what you are after:
string pattern = " ^getelementbyid\\(\"(.*?)\" ";
Upvotes: 0