【问题标题】:Parenthesis text capturing (Perl RegEx)括号文本捕获 (Perl RegEx)
【发布时间】:2014-01-28 15:42:39
【问题描述】:

我回来跟进this question。 假设我有文字

====Example 1====
Some text that I want to get that
may include line breaks
or special ~!@#$%^&*() characters

====Example 2====
Some more text that I don't want to get.

并使用$output = ($text =~ ====Example 1====\s*(.*?)\s*====); 尝试获取从“====Example 1====”到“Example 2”之前的四个等号的所有内容。

根据我所看到的on this siteregexpal.com 并通过自己运行它,Perl 找到并匹配文本,但 $output 仍然为空或被分配为“1”。我很确定我在捕获括号上做错了什么,但我不知道是什么。任何帮助,将不胜感激。 我的完整代码是:

$text = "====Example 1====\n
Some text that I want to get this text\n
may include line breaks\n
or special ~!@#$%^&*() characters\n
\n
====Example 2====]\n
Some more filler text that I don't want to get.";
my ($output) = $text =~ /====Example 1====\s*(.*?)\s*====/;
die "un-defined" unless defined $output;
print $output;

【问题讨论】:

  • 我的 ($output) = $text =~ /====示例 1====\s*(.*?)\s*====/;

标签: regex perl


【解决方案1】:

尝试使用括号强制列表上下文,并在匹配时使用/s,以便.也可以匹配换行符,

my ($output) = $text =~ / /s;

【讨论】:

  • 试过了,但$output 仍然为空。
  • 完美运行!谢谢!
【解决方案2】:

两件事。

  1. 将 /s 标志应用于正则表达式,让它知道正则表达式的输入可能是多行。
  2. 将括号改为$output,而不是($text ~= regex);

例子:

($output) = $text =~ /====Example\s1====\s*(.*?)\s*====/s;

例如,将其放入如下脚本中:

#!/usr/bin/env perl

$text="
====Example 1====
Some text that I want to get that
may include line breaks
or special ~!@#$%^&*() characters

====Example 2====
Some more text that I don't want to get.
";

print "full text:","\n";
&hr;
print "$text","\n";
&hr;

($output) = $text =~ /====Example\s1====\s*(.*?)\s*====/s;
print "desired output of regex:","\n";
&hr;
print "$output","\n";
&hr;

sub hr {
        print "-" x 80, "\n";
}

让你输出如下:

bash$ perl test.pl
--------------------------------------------------------------------------------
full text:
--------------------------------------------------------------------------------

====Example 1====
Some text that I want to get that
may include line breaks
or special ~!@#0^&*() characters

====Example 2====
Some more text that I don't want to get.

--------------------------------------------------------------------------------
desired output of regex:
--------------------------------------------------------------------------------
Some text that I want to get that
may include line breaks
or special ~!@#0^&*() characters
--------------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 2016-06-03
    • 2013-04-25
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多