title: Best way to parse bbcode
body: <p>I'd like to work on a bbcode filter for a php website. (I'm using cakephp, it would be a bbcode helper)
  I have some requirement.  </p>
  
  <p><strong>Bbcodes can be nested. So something like that is valid.</strong></p>
  
  <pre><code>[block]  
      [block]  
      [/block]  
      [block]  
          [block]  
          [/block]  
      [/block]  
  [/block]  
  </code></pre>
  
  <p><strong>Bbcodes can have 0 or more parameters.</strong>   </p>
  
  <p>Exemple:   </p>
  
  <pre><code>[video: url="url", width="500", height="500"]Title[/video]
  </code></pre>
  
  <p><strong>Bbcodes might have mutliple behaviours.</strong>   </p>
  
  <p>Let say, <code>[url]text[/url]</code> would be transformed to <code>[url:url="text"]text[/url]</code>
  or the video bbcode would be able to choose between youtube, dailymotion....</p>
  
  <p>I think it cover most of my needs. I alreay done something with regex. But my biggest problem was to match parameters. In fact, I got nested bbcode to work and bbcode with 0 parameters. But when I added a regex match for parameters it didn't match nested bbcode correctly. </p>
  
  <p><code>"\[($tag)(=.*)\"\](.*)\[\/\1\]"</code>  // It wasn't .* but the non-gready matcher</p>
  
  <p>I don't have the complete regex with me right now, But I had something that looked like that(above).</p>
  
  <p>So is there a way to match bbcode efficiently with regex or something else. 
  The only thing I can think of is to use the visitor pattern and to split my text with each possible tags this way, I can have a bit more of control over my text parsing and I could probably validate my document so if the input text doesn't have valid bbcode. I could Notify the user with a error before saving anything.</p>
  
  <p>I would use sablecc to create my text parser.
  <a href="http://sablecc.org/" rel="noreferrer">http://sablecc.org/</a></p>
  
  <p>Any better idea? or anything that could lead to a efficient flexible bbcode parser?</p>
  
  <p>Thank you and sorry for my bad english...</p>