0

I've been trying all day with no luck to regex replace the new line "\n" with a "<br />" but outside a code block like a "**[code]**some code...**[/code]**".

For example if I have the following

string myString = "Good luck **\n** thanks [code]my **\n** name[/code]";

How to replace the line breaks "\n" with "<br />" outside the code tag only, using regular expression?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Mohamed Tarek
  • 111
  • 4
  • 11

2 Answers2

2

BBCode is not a regular language, so using regular expressions to parse it is not the best approach.

Look at writing a parser based on the character stream - take a look at the source code from the HTML Agility Pack to see how this is done (basically a state machine moving from character to character).

Alternatively, use one of the existing .NET BBCode parsers, as the answers to BBCode or wiki markup libraries for .NET? point to.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks, can you give me a quick example in c# for example replacing the [b]this is bold[/b] to this is bold using the HTML Agility Pack ? – Mohamed Tarek Mar 29 '12 at 15:59
  • @MohamedTarek - No. The HTML Agility Pack is an _HTML_ parser. I pointed you to it as an example of how to write a parser. – Oded Mar 29 '12 at 16:00
  • Forgive my bad English I'm an ESL but I meant the regex pattern to replace all \n in a string with
    except for the onse in [code] block.
    – Mohamed Tarek Mar 29 '12 at 16:05
  • @MohamedTarek - My answer is that regex is not appropriate (not good) for this. – Oded Mar 29 '12 at 16:10
0

Well, after trying and failing so many times I came up with the following code and it worked, it's vb.net BTW.

Public Function BBCode(ByVal strTextToReplace As String) As String
        Dim regExp As Regex
        strTextToReplace = strTextToReplace.Replace(Chr(10), "<br />")

        regExp = New Regex("\[code\]([^\]]+)\[\/code\]", RegexOptions.Singleline)
        Dim mc As MatchCollection = regExp.Matches(strTextToReplace)
        For Each m As Match In mc
            Dim gs = m.Groups()
            strTextToReplace = regExp.Replace(strTextToReplace, "<table cellpadding=""2"" cellspacing=""0"" class=""tbl_Quote""><tr><td dir=""rtl""><a class=""quote_Head"">code:</a></td></tr><tr><td><textarea class=""lined"" cols=""77"" readonly=""readonly"" rows=""1"" spellcheck=""false"">" & gs(1).Value.ToString.Replace("<br />", Chr(10)) & "</textarea></td></tr></table>", 1)
        Next
Return strTextToReplace
End Function

I hope this helps someone else.

Mohamed Tarek
  • 111
  • 4
  • 11
  • This may very well fail if your BBCode is not well formed. That's why I suggested using an existing parser. – Oded Apr 03 '12 at 18:25