Tag: 多线

Ruby:逐行匹配范围

有没有办法在Ruby中执行以下Perl结构? while( my $line = $file->each_line() ) { if($line =~ /first_line/ .. /end_line_I_care_about/) { do_something; # this will do something on a line per line basis on the range of the match } } 在ruby中会读到如下内容: file.each_line do |line| if line.match(/first_line/) .. line.match(/end_line_I_care_about/) do_something; # this will only do it based on the first match not […]

如何在Ruby中修复此多行正则表达式?

我在Ruby中有一个正则表达式,它在多行模式下无法正常工作。 我正在尝试将Markdown文本转换为Redmine中使用的Textile-eque标记。 问题出在我的正则表达式中,用于转换代码块。 它应该找到带有4个空格或制表符的任何行,然后将它们包装在预标签中。 markdownText = ‘# header some text that precedes code var foo = 9; var fn = function() {} fn(); some post text’ puts markdownText.gsub!(/(^(?:\s{4}|\t).*?$)+/m,”\n\\1\n “) 预期结果: # header some text that precedes code var foo = 9; var fn = function() {} fn(); some post text 问题是关闭预标记打印在文档的末尾而不是“fn();”之后。 我尝试了以下表达式的一些变体,但它不匹配: gsub!(/(^(?:\s{4}|\t).*?$)+^(\S)/m, “\n\\1\n \\2”) […]