Good afternoon, dear users of Habr.
In this post I would like to share with what was played for a long time and I hope it will be useful to someone.
Recently I faced the fact that in the new project it was necessary to make syntax highlighting for MarkDown markup language. Recently, it is quite popular in different places. Googled for a long time, as a result, I had to write everything myself and for a long time to test everything to work correctly.
Under the cut are a set of regular expressions that are looking for the basic markdown markup elements in the variant for Objective-c.
H1
^
H2
^#{2,2} (.)+$ // ^.+$\n^-{3,}$ //
H3 ... H6
^#{3,3} (.)+$ ... ^#{6,6} (.)+$
bold (** Text **)
(?<!\*|\\\*)\*{2,2}[^\*\n].+?[^\*]\*{2,2}(?!\*|\\)
italic (* Text * or _text_)
((?<!\*|\\)\*[^\*\n].+?[^\*|\\]\*(?!\*))|(_.+?_)
bold italic (*** Text ***)
(?<!\*|\\)\*{3,3}[^\*\n].+?[^\*|\\]\*{3,3}(?!\*)
monospace (`text`)
`[^`]*`
Numbered list
(^\d{1,3}\. .*$)+|(^ {1,10}\d{1,3}\. .*$)+
Unnumbered list
(^(\*|\+|-) .*$)+
image! [img alt] (http: //image.url/img.png)
!\[[^\[\]]*?\]\(.*?\)
url [link] (http://site.com/)
\[[^\[\]]*?\]\(.*?\)|^\[*?\]\(.*?\)
block quotes
^>{1,4} (.)+$
')
Here is a piece of the screenshot from the application in which these regular expressions were applied.
