2013-08-19 9 views
5

私の入力はfn(a(b,c),d) fn(a,d) fn(a(b),d)で、私はa(b,c),dとしたいと思います。第二FN()は最初に簡単で、第三私はあなたがこのためにbalancing group definitionsを必要とするどのようRegexは `)`のトラックを維持します

+0

あなたは空白のchセパレータとしてのアトラクタ?あなたは 'fn [(](\ S *)[)]'を試すことができます。 – abiessu

+0

'fn'はさらに' fn's、 'a' /' b'/'c'sだけを含むことができますか?入力には多くの 'fn'sが含まれていますか?多くの人がいるならば、彼らは単に列に並んでいるのだろうか、あるいはそれらの間に他のものがあるのだろうか?入れ子の深さは 'fn(a(b(c)))'となりますか? – Kobi

答えて

5

を一致させるために知っていない:あなたはそれを

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

を分割することができます

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

あなたは出力を得るでしょう

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

あなたのパターンは ')'と '' 'の間で何かを分割します。' 'fn(a()、b())'や他の多くのバリエーションをうまく分割します(もちろん、可能) – Kobi

+0

@Kobi yes..indeed..ur right..opこの可能性を確認してください。 – Anirudha

関連する問題