0

I need to parse something like BBCode and use this regular expression: \[b\](.+?)\[\/b\]

But this expression finds only the first occurrence if input text contains embedded bbcodes. In this case, i need to find all matches.

Community
  • 1
  • 1
Maxim
  • 1,413
  • 2
  • 10
  • 15
  • 1
    For better understanding, Please provide a **working demo** (*code snippet,jsfiddle ...*) – Kishore Sahasranaman Dec 06 '15 at 11:08
  • A [b] Tag inside a [b] tag shouldn't be valid anyways, right? You can fix your regex a little bit by using `[\s\S]` (matches everything) instead of `.` (matches everything *except linebreaks*). But overall, you shouldn't be parsing complex bb-codes with Regexes anyway, have a look at https://stackoverflow.com/questions/12936622/bb-code-regex-in-javascript. – Maximilian Gerhardt Dec 06 '15 at 11:13
  • See [this question](http://stackoverflow.com/q/19596502/335858) for an explanation of how this can be done using C# regex. This solution is specific to .NET, so you wouldn't be able to test it with, say, regex101. – Sergey Kalinichenko Dec 06 '15 at 11:14
  • Here is an excellent article explaining the solution in the above comments in depth: http://www.codeproject.com/Articles/21080/In-Depth-with-RegEx-Matching-Nested-Constructions Helped me a few times, but generally when working with a nested structure like HTML, XML and the like, regex isn't the right tool. – Henrik Nielsen Dec 06 '15 at 11:17
  • 1
    First, do not use regex101.com if you need a .NET compatible regex. Second, you may use balanced groups in .NET regex to match balanced constructs. E.g. [`\[b]((?>\[b](?)|\[/b](?<-DEPTH>)|.)*(?(DEPTH)(?!)))\[/b]`](http://regexstorm.net/tester?p=%5c%5bb%5d((%3f%3e%5c%5bb%5d(%3f%3cDEPTH%3e)%7c%5c%5b%2fb%5d(%3f%3c-DEPTH%3e)%7c.)*(%3f(DEPTH)(%3f!)))%5c%5b%2fb%5d&i=%5bb%5d+%0d%0a++++%5bb%5dexample%5b%2fb%5d%0d%0a%5b%2fb%5d%0d%0a%5bb%5d+%0d%0a++++%5bb%5d%0d%0a++++++%5bb%5dexample2%5b%2fb%5d%0d%0a++++%5b%2fb%5d%0d%0a%5b%2fb%5d&o=s). However, why not use a BBCode parsing library? – Wiktor Stribiżew Dec 06 '15 at 12:22

0 Answers0