如何在Ruby中修复此多行正则表达式? [英] How do I fix this multiline regular expression in Ruby?

查看:113
本文介绍了如何在Ruby中修复此多行正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ruby中有一个正则表达式,在多行模式下无法正常工作.

I have a regular expression in Ruby that isn't working properly in multiline mode.

我正在尝试将Markdown文本转换为Redmine中使用的Textile-eque标记.问题出在我转换代码块的正则表达式中.它应该找到以4个空格或一个制表符开头的任何行,然后将其包装在 pre 标记中.

I'm trying to convert Markdown text into the Textile-eque markup used in Redmine. The problem is in my regular expression for converting code blocks. It should find any lines leading with 4 spaces or a tab, then wrap them in pre tags.

markdownText = '# header

some text that precedes code

    var foo = 9;
    var fn = function() {}

    fn();

some post text'

puts markdownText.gsub!(/(^(?:\s{4}|\t).*?$)+/m,"<pre>\n\\1\n</pre>")

预期结果:

# header

some text that precedes code

<pre>
    var foo = 9;
    var fn = function() {}

    fn();
</pre>

some post text

问题是结束符 pre 标记打印在文档的末尾,而不是在"fn();"之后.我尝试了以下表达式的一些变体,但不匹配:

The problem is that the closing pre tag is printed at the end of the document instead of after "fn();". I tried some variations of the following expression but it doesn't match:

gsub!(/(^(?:\s{4}|\t).*?$)+^(\S)/m, "<pre>\n\\1\n</pre>\\2")

如何获取仅与缩进代码块匹配的正则表达式?您可以在Rubular 此处上测试此正则表达式.

How do I get the regular expression to match just the indented code block? You can test this regular expression on Rubular here.

推荐答案

首先,请注意Ruby中的'm'多行模式等效于其他语言的's'单行模式.换一种说法; Ruby中的'm'模式表示:点匹配所有" .

First, note that 'm' multi-line mode in Ruby is equivalent to 's' single-line mode of other languages. In other words; 'm' mode in Ruby means: "dot matches all".

此正则表达式将很好地匹配类似于markdown的代码段:

This regex will do a pretty good job of matching a markdown-like code section:

re = / # Match a MARKDOWN CODE section.
    (\r?\n)              # $1: CODE must be preceded by blank line
    (                    # $2: CODE contents
      (?:                # Group for multiple lines of code.
        (?:\r?\n)+       # Each line preceded by a newline,
        (?:[ ]{4}|\t).*  # and begins with four spaces or tab.
      )+                 # One or more CODE lines
      \r?\n              # CODE folowed by blank line.
    )                    # End $2: CODE contents
    (?=\r?\n)            # CODE folowed by blank line.
    /x
result = subject.gsub(re, '\1<pre>\2</pre>')

这需要在代码段之前和之后留空行,并允许代码段本身内包含空行.它允许\r\n\n线路终端.请注意,这不会在每行之前去除前导4个空格(或制表符).这样做将需要更多的代码复杂性. (我不是一个红宝石家伙,所以对此无能为力.)

This requires a blank line before and after the code section and allows blank lines within the code section itself. It allows for either \r\n or \n line terminations. Note that this does not strip the leading 4 spaces (or tab) before each line. Doing that will require more code complexity. (I am not a ruby guy so can't help out with that.)

我建议您查看降价来源本身,以了解其实际效果.

I would recommend looking at the markdown source itself to see how its really being done.

这篇关于如何在Ruby中修复此多行正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆