使用正则表达式来修复降价输入 - 链接标签

Jon*_*sco 5 python regex markdown

我们遇到了一些降价内容的问题.我们使用的一些jquery编辑器没有编写正确的markdown语法.Embedded Links使用'label'格式,它会删除文档底部的链接(就像StackOverflow编辑器一样).我们遇到的问题是链接有时以非标准方式格式化.虽然允许它们以0,3个空格为前缀,但有些空格有4个空格(你可能会注意到StackOverflow在javascript中强制使用2个空格) - 这会preformatted text在markdown解析器中触发它.

作为一个简单的例子:

This is a sample doucument that would have inline links. 
[Example 0][0], [Example 1][1], [Example 2][2] , [Example 3][3] , [Example 4][4]

[0]: http://example.com
 [1]:      http://example.com/1
  [2] : http://example.com/2
   [3]: http://example.com/3
    [4]  : http://example.com/4
Run Code Online (Sandbox Code Playgroud)

我想将最后一节重新格式化为正确的降价:

[0]: http://example.com
[1]: http://example.com/1
[2]: http://example.com/2
[3]: http://example.com/3
[4]: http://example.com/4
Run Code Online (Sandbox Code Playgroud)

我正在试图想出正确的正则表达式来抓住'标签'部分.我可以很好地抓住该部分内的标签 - 但该部分正在躲避我.

这是我到目前为止所拥有的:

RE_footnote = re.compile("""
    (?P<labels_section>
        ^[\t\ ]*$                             ## we must start with an empty line
        \s+                       
        (?P<labels>
            (?P<a_label>
                ^
                    [\ \t]*                     ## we could have 0-n spaces or tabs
                    \[                          ## BRACKET - open
                        (?P<id>
                            [^^\]]+
                        )
                    \]                          ## BRACKET - close
                    \s*
                    :                           ## COLON
                    \s*
                    (?P<link>                   ## WE want anything here
                        [^$]+
                    )
                $
            )+                                  ## multiple labels
        )
    )
""",re.VERBOSE|re.I|re.M)
Run Code Online (Sandbox Code Playgroud)

我遇到的具体问题:

  1. 我无法弄清楚如何允许1个或更多"空行".这会触发无效的正则表达式,无需重复:

    (?:##将它包装在非捕获组中,需要1+次出现^ [\ t \]*$
    )+

  2. 如果没有在组之前的空白匹配,则匹配将不起作用\s+.我无法弄清楚是什么/为什么.

  3. 我希望这只匹配文档的END,以确保我们只修复这些javascript错误(而不是文档的核心).所有我努力工作的尝试\z都失败了,悲惨地.

有人可以提供一些建议吗?


更新

这工作:

RE_MARKDOWN_footnote = re.compile("""
    (?P<labels_section>
        (?:                            ## we must start with an empty / whitepace-only line
            ^\s*$
        )                              
        \s*                             ## there can be more whitespace lines
        (?P<labels>
            (?P<a_label>
                ^
                    [\ \t]*                     ## we could have 0-n spaces or tabs
                    \[                          ## BRACKET - open
                        (?P<id>
                            [^^\]]+
                        )
                    \]                          ## BRACKET - close
                    \s*
                    :                           ## COLON
                    \s*
                    (?P<link>                   ## WE want anything here
                        [^$]+
                    )
                $
            )+                                  ## multiple labels
        )
        \s*                                     ## we might have some empty lines 
        \Z                                      ## ensure the end of document
    )
""",re.VERBOSE|re.I|re.M)
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 1

我刚刚从头开始;有没有像这样简单的东西无法工作的原因?

^\s*                # beginning of the line; may include whitespace
  \[                # opening bracket
     (?P<id>\d+)    # our ID
  \]                # closing bracket
\s*                 # optional whitespace
  :                 # colon
\s*                 # optional whitespace
  (?P<link>[^\n]+)  # our link is everything up to a new line
$                   # end of the line
Run Code Online (Sandbox Code Playgroud)

这是使用全局和多行修饰符gm. 将匹配项替换为:[\id]: \link。这是一个工作示例:http://regex101.com/r/mM8dI2