1

I want to match var value between quotes. here's input:

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

result should be 1354010802-f407561a1503fa33e8e316f058c0f0598ce5adad

i was trying something like this: Match m = Regex.Match(input, @"var\sSECURITYTOKEN\s="); but im totally confused.

dovydas juraska
  • 277
  • 2
  • 5
  • 12
  • try this: http://stackoverflow.com/questions/375104/how-can-i-match-a-quote-delimited-string-with-a-regex – speti43 Nov 27 '12 at 11:16
  • A good resource for testing .Net regex patterns online : http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx – hoang Nov 27 '12 at 12:38

3 Answers3

3

Your variant finds only the part var SECURITYTOKEN =

use positive lookahead (?=...) and positive lookbehind (?<=...)

String regexPattern = "(?<=var SECURITYTOKEN = \")(.*?)(?=\")";
Match m = Regex.Match(input, regexPattern);
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
0

Try this regex (?<=")(.*?)(?=")

So it will be something like this

var regexPattern = "(?<=\")(.*?)(?=\")";
Match m = Regex.Match(input, regexPattern);
og Grand
  • 114
  • 3
0

This is pretty much the same as og Grand answer but if your input looks like

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

the following will be better :

var regexPattern = "(?<=var SECURITYTOKEN\s*=\s*\")(.*?)(?=\";)";
Match m = Regex.Match(input, regexPattern);
hoang
  • 1,887
  • 1
  • 24
  • 34