今天一天折腾了这么一个正则
new Regex(@"^!?\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\s*\(\s*<?((?:[^()]|\((?<DEPTH>)|\)(?<-DEPTH>))+?(?(DEPTH)(?!)))>?(?:\s+(['""])([\s\S]*?)\3)?\s*\)");
话说这个正则是干什么用的,故事是这样的:
很久很久以前
我在markdownlite里面抄了个marked里面的正则
一切都很好。
突然,
有用户报我们的markdown不对,
什么情况?
一问是link的处理有问题。
是什么奇葩link那?
打开一看,
映入眼帘的是:
[hello](foo(bar).md)
marked里面的正则会处理成:
<a href="foo(bar">hello</a>bar).md)
确实bar后面都跟)了链接就到处为止了,
看起来是用户内容有问题啊!
再跑到github里面一试,
傻眼了:
<a href="foo(bar).md">hello</a>
吐血中。。。
再试了很多case后,
发现github只要在link target里面的的()是成对出现的就能work。
于是就有了今天这个正则。
这个复杂的正则需要这么去理解:
^ start of string
!? '!' 0~1
\[ '['
((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*) group 1: text
\] ']'
\s* white spaces
\( '('
\s* white spaces
<? '<' 0~1
( start group 2: link
(?: start non-capturing group
[^()] any chararacter but '(' or ')'
| or
\((?<DEPTH>) '(' with depth++
| or
\)(?<-DEPTH>) ')' with depth--
) end non-capturing group
+? lazy 1~
(?(DEPTH)(?!)) require depth = 0
) end group 2: link
>? '>' 0~1
(?: start non-capturing group
\s+ white spaces
(['"]) group 3: quote ' or "
([\s\S]*?) group 4: title
\3 ref group 3
)? end non-capturing group 0~1
\s* white spaces
\) ')'
浙公网安备 33010602011771号