今天折腾这么一个正则
2016-02-16 23:19
375 查看
今天一天折腾了这么一个正则
话说这个正则是干什么用的,故事是这样的:
很久很久以前
我在markdownlite里面抄了个marked里面的正则
一切都很好。
突然,
有用户报我们的markdown不对,
什么情况?
一问是link的处理有问题。
是什么奇葩link那?
打开一看,
映入眼帘的是:
marked里面的正则会处理成:
确实bar后面都跟)了链接就到处为止了,
看起来是用户内容有问题啊!
再跑到github里面一试,
傻眼了:
吐血中。。。
再试了很多case后,
发现github只要在link target里面的的()是成对出现的就能work。
于是就有了今天这个正则。
这个复杂的正则需要这么去理解:
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 \) ')'
相关文章推荐
- BZOJ 2251 Beijing WC 2010 外星联络 后缀数组
- Winedt常用配置
- Windows系统Stunnel客户端的配置
- Building Maintainable Software-java篇之 Write Clean Code
- 【hadoop】win7-32位下安装hadoop2.x
- java 设计模式之策略模式
- 删除Xcode中多余的证书provisioning profile
- 开发技术组件官网教程清单
- 2016给自己一个交代
- USACO 1.4 Search Techniques
- UVA 156
- leetcode279-Perfect Squares
- struts框架搭建解决方案
- hdu 4389 数位DP 模板化代码 结合记忆化搜索优化
- linux-信号量
- Python基本数据类型
- 4.14-1 图片太大遇到的问题,Bitmap too large to be uploaded into a texture (2340x4160, max=4096x4096)
- 3--单链表
- Python基本数据类型
- iOS开发UI篇—UIWindow简单介绍