简体   繁体   English

我正在尝试使用C#创建与Trac Wiki兼容的简单Wiki解析器,但是正则表达式使我丧命

[英]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+|$))

正则表达式可视化

Debuggex Demo Debuggex演示

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. RegEx与trac内部使用的几乎相同。

Your regex pattern is correct. 您的正则表达式模式正确。 You should just switch on "dot matches linebreak" modifier or put this (?:.|[\\r\\n]) instead of just . 您应该只打开“点匹配换行符”修饰符,或放置(?:.|[\\r\\n])而不是just . (dot). (点)。 Try this out: 试试看:

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

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM