1

我想匹配引号之间的 var 值。这是输入:

bunch of other text, html tags, css 
var SECURITYTOKEN = "1354010802-f407561a1503fa33e8e316f058c0f0598ce5adad";
bunch of other text, html tags, css 

结果应该是 1354010802-f407561a1503fa33e8e316f058c0f0598ce5adad

我正在尝试这样的事情: Match m = Regex.Match(input, @"var\sSECURITYTOKEN\s="); 但我完全糊涂了。

4

3 回答 3

3

您的变体仅找到部分var SECURITYTOKEN =

使用积极的前瞻(?=...)和积极的后瞻(?<=...)

String regexPattern = "(?<=var SECURITYTOKEN = \")(.*?)(?=\")";
Match m = Regex.Match(input, regexPattern);
于 2012-11-27T11:30:14.703 回答
0

试试这个正则表达式 (?<=")(.*?)(?=")

所以它会是这样的

var regexPattern = "(?<=\")(.*?)(?=\")";
Match m = Regex.Match(input, regexPattern);
于 2012-11-27T11:17:33.717 回答
0

这与 og Grand answer 几乎相同,但如果您的输入看起来像

var foo = "bar";
var SECURITYTOKEN = "1354010802-f407561a1503fa33e8e316f058c0f0598ce5adad";
var tata = "titi";

以下会更好:

var regexPattern = "(?<=var SECURITYTOKEN\s*=\s*\")(.*?)(?=\";)";
Match m = Regex.Match(input, regexPattern);
于 2012-11-27T12:35:21.770 回答