2013-08-19 2 views
5

내 입력이 fn(a(b,c),d) fn(a,d) fn(a(b),d)이고 내가 a(b,c),d을 (를) 내부의 모든 것을 가져 오는 패턴을 작성하고 싶습니다. 제 2 FN()는 첫번째 용이하고 세 번째 나는 당신이에 대한 balancing group definitions 필요Regex는`)`를 추적합니다

+0

으로 표시됩니다. 공백 채널이 주어 졌습니까? 분리기로 aracter? 당신은'fn [(] (\ S *) [)]'을 시도 할 수있다. – abiessu

+0

'fn'은 더 많은'fn's 또는'a' /'b' /'c's만을 포함 할 수 있습니까? 입력에 많은'fn's 또는 하나만 들어 있습니까? 많은 사람들이 있다면, 그들은 단지'fn' 연속적으로 존재합니까, 아니면 그들 사이에 다른 것이있을 수 있습니까? 중첩은 얼마나 깊습니까?'fn (a (b (c)))'가 유효합니까? – Kobi

답변

5

일치하는 방법을 모른다 :

result = Regex.Match(subject, 
    @"(?<=\()    # Make sure there's a (before the start of the match 
     (?>    # now match... 
      [^()]+   # any characters except parens 
     |     # or 
      \( (?<DEPTH>) # a (, increasing the depth counter 
     |     # or 
      \) (?<-DEPTH>) # a), decreasing the depth counter 
     )*     # any number of times 
     (?(DEPTH)(?!))  # until the depth counter is zero again 
     (?=\))    # Make sure there's a) after the end of the match", 
    RegexOptions.IgnorePatternWhitespace).Value; 
2

당신은 그것을

var output=Regex.Split(input,@"(?:\)|^)[^()]*(?:\(|$)"); 

을 분할 할 수 있습니다 출력은

a(b,()c),d 
a,d 
a(b),d 
+0

당신의 패턴은')'와'('사이에있는 것들을 나눕니다.''fn (a(), b())'및 다른 많은 변형을 깔끔하게 분할합니다. (물론, 가능) – Kobi

+0

@Kobi yes..indeed..ur right..op이 가능성을 확인해야합니다. – Anirudha

관련 문제