2012-11-27 2 views
1

따옴표 사이에 var 값을 일치시키고 싶습니다.C# 정규식 일치 var 값

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

결과가 있어야한다 1,354,010,802 - f407561a1503fa33e8e316f058c0f0598ce5adad

내가이 같은 노력했다 : 여기에 입력의 매치 m = Regex.Match (@ 입력, "VAR을 \ sSECURITYTOKEN \ S ="); 하지만 완전히 혼란 스럽습니다.

+0

이 시도 : http://stackoverflow.com/questions/375104/how-can-i-match-a-quote-delimited-with-a-regex – speti43

+0

테스트를위한 좋은 리소스. 닷넷 정규식 패턴 온라인 : http://derekslager.com/blog/posts/2007/ 09/a-better-dotnet-regular-expression-tester.ashx – hoang

답변

3

같은 것이 정규식을 시도 var SECURITYTOKEN =

사용 긍정적 예측 (?=...) 긍정적 인 lookbehind (?<=...)

String regexPattern = "(?<=var SECURITYTOKEN = \")(.*?)(?=\")"; 
Match m = Regex.Match(input, regexPattern); 
,210
+0

감사합니다. 완벽하게 작동합니다! –

+0

+1 그 대답은 :) –

0

귀하의 변형이 부분 만 찾아 (?<=")(.*?)(?=")

는 그래서이

var regexPattern = "(?<=\")(.*?)(?=\")"; 
Match m = Regex.Match(input, regexPattern); 
0

이 꽤 많이 OG 그랜드 대답과 동일하지만 입력

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

처럼 보이는 경우 다음이 더 좋을 것이다 :

var regexPattern = "(?<=var SECURITYTOKEN\s*=\s*\")(.*?)(?=\";)"; 
Match m = Regex.Match(input, regexPattern);