Search code examples
c#regexparsingwiki

I'm trying to create simple wiki parser in C# that would be compatible with trac wiki, but regex is killing me


I need to match non parsed parts first, so I created regex that should catch them:

(?<inlinecode>!?\{\{\{(?<inline>.*?)\}\}\})|(?<inlinecode2>!?`(?<inline2>.*?)`)|(?<definition>^\s+((?:`[^`]*`|\{\{\{(?:}{,2}[^}])*?\}\}\}|[^`{:]|:[^:])+::)(?:\s+|$))

Regular expression visualization

Debuggex Demo

This part gets matched:

`test` 
{{{test}}}
`{{{test`
{{{``test}}}

But this not (and it should):

{{{
test
}}}

{{{#!xxx
test
}}}

def::
  some def

What am I missing. RegEx is almost the same as trac uses internally.


Solution

  • Your regex pattern is correct. You should just switch on "dot matches linebreak" modifier or put this (?:.|[\r\n]) instead of just .(dot). Try this out:

    (?<inlinecode>!?\{\{\{(?<inline>(?:.|[\r\n])*?)\}\}\})|(?<inlinecode2>!?`(?<inline2>(?:.|[\r\n])*?)`)|(?<definition>^\s+((?:`[^`]*`|\{\{\{(?:}{,2}[^}])*?\}\}\}|[^`{:]|:[^:])+::)(?:\s+|$))
    

    Cheers.