title: .Net Regex - Help needed
body: <h2>Updated</h2>
  
  <p>I need help with a regex that will find the <code>"@[..[...]]"</code> pattern.</p>
  
  <p><b>I will try to explain.</b></p>
  
  <p>A text will contain placeholders which will be replaced with values upon display of that very same text.</p>
  
  <p>A place holder has 3 parts;</p>
  
  <ul>
  <li>an open tag, starts with "@[" followed by "a dot delimited text" and ends with "[",</li>
  <li>a property list, a "comma separated list" with qouted (double qoutes) values,</li>
  <li>a close tag, "]]".</li>
  </ul>
  
  <p>The property list items can contain one or many placeholders (nested) and both double qoutes (escaped) and brackets.</p>
  
  <p>The regex must overcome the issues with nested placeholders by knowing when it reached the end of the "outer" placeholder as well as any escaped qoutes and brackets.</p>
  
  <p><b>Sample</b></p>
  
  <p>Consider the following text fragment:</p>
  
  <pre><code>Linklist    
  @[Link.AppText["[startpage]", "startpage"]]
  @[Link.Text["[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"]]
  @[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
  </code></pre>
  
  <p>The match should look like this:</p>
  
  <pre><code>match 1  =  @[Link.AppText["[startpage]", "startpage"]]
     Gr.1  =  Link.AppText
     Gr.2  =  "[startpage]", "startpage"
  
  match 2  =  @[Link.Text["[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"]]
     Gr.1  =  Link.Text
     Gr.2  =  "[startpage] loggedin", "The \"@[Text.AppText["startpage"]]\" for users"
  
  match 3  =  @[Link.Text["@[Link["startpage"]]", "@[Text.AppText["startpage"]]"]]
     Gr.1  =  Link.Text
     Gr.2  =  "@[Link["startpage"]]", "@[Text.AppText["startpage"]]"
  </code></pre>
  
  <p><b>With a solution by @ridgerunner I solved it:</b></p>
  
  <pre><code>@\[([._\w]+)\[([^[\]""]*(?:""[^""\\]*(?:\\.[^""\\]*)*""[^[\]""]*)*)\]\]
  
  @\[                                # Outer open delimiter.
  ([._\w]+)                          # 1:st group.
  \[                                 # Inner open delimiter.
  (                                  # Start of 2:nd group.
  [^[\]""]*                          # Contents.
  (?:""[^""\\]*(?:\\.[^""\\]*)*""    # Contents.
  [^[\]""]*)*                        # Contents.
  )                                  # End of 2:nd group.
  \]\]                               # Close delimiter.
  </code></pre>
  
  <p><b>And ... for anyone who looks for a "balanced group solution"</b></p>
  
  <p>... after struggling with google search and a lot of regex testing, I finally figured out another working solution, though I had to alter the pattern slightly to make it work: (at least for me :))</p>
  
  <pre>
  Regex:  @([._\w]+)\[\[(""(?:[^\[\]]*|\[[^\[]|[^\]]\]|(?&lt;counter&gt;\[\[)|(?&lt;-counter&gt;\]\]))+(?(counter)(?!))"")\]\]
  
  @([._\w]+)\[\[            #   start tag, 1:st group
    (""                     #   start 2:nd group
      (?:                   #   non capturing group
        [^\[\]]*            #   any char but [ or ]
        |                   #   or
        \[[^\[]             #   if [, not followed by a [
        |                   #   or
        [^\]]\]             #   if ], not followed by a ]
        |                   #   or
        (?&lt;counter&gt;\[\[)    #   counter start tag
        |                   #   or
        (?&lt;-counter&gt;\]\])   #   counter stop tag
      )+                    #   end non capturing group
      (?(counter)(?!))      #   if counter &lt;&gt; 0, regex fails
    "")                     #   end 2:nd group
  \]\]                      #   end tag
  </pre>
  
  <p>Updated placeholders with new pattern; <code>(@..[[...]]</code></p>
  
  <pre>
  Linklist
  @Link.AppText[["[startpage]", "startpage"]]
  @Link.Text[["[startpage] loggedin", "The \"@Text.AppText[["startpage"]]\" for users"]]
  @Link.Text[["@Link[["startpage"]]", "@Text.AppText[["startpage"]]"]]
  </pre>