Perl one liner提取多线图案

Gil*_*Gil 8 bash perl awk sed perl-module

我在文件中有一个模式如下,它可以/不能跨越多行:

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

perl -nle'打印时m/^\s*(\ w +)\ s +(\ w +?)\ s*(([\ w-0-9,*\s] ))\ s {/ gm'

我不知道这里的标志是什么意思,但我所做的只是regex为模式写一个并将其插入模式空间.如果模式在一行中,则匹配良好:

abcd25 ef_gh ( fg*_h hj_b* hj ) {
Run Code Online (Sandbox Code Playgroud)

但仅在多线情况下失败!

我昨天开始使用perl,但语法太混乱了.因此,正如我们的SO伙伴之一所建议的那样,我写了一个regex并将其插入到他提供的代码中.

我希望perl在这种情况下,和尚可以帮助我.欢迎替代解决方案.

输入文件 :

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {

 abcd25
 ef_gh
 fg*_h
 hj_b*
 hj ) {

 jhijdsiokdù ()lmolmlxjk;
 abcd25 ef_gh ( fg*_h hj_b* hj ) {
Run Code Online (Sandbox Code Playgroud)

预期产量:

 abcd25
 ef_gh
 ( fg*_h
 hj_b*
 hj ) {
 abcd25 ef_gh ( fg*_h hj_b* hj ) {
Run Code Online (Sandbox Code Playgroud)

输入文件可以具有多个模式,这些模式与所需模式的开始和结束模式一致.在此先感谢您的回复.

cho*_*oba 6

正则表达式甚至不匹配单行.你认为双括号怎么办?

你可能想要

m/^\s*(\w+)\s+(\w+?)\s*\([\w0-9,*\s]+\)\s{/gm
Run Code Online (Sandbox Code Playgroud)

更新:规范已更改.正则表达式(几乎)没有,但您必须稍微更改代码:

perl -0777 -nle 'print "$1\n" while m/^\s*(\w+\s+\w+?\s*\([\w0-9,*\s]+\)\s{)/gm'
Run Code Online (Sandbox Code Playgroud)

另一个更新:

说明:

  • 开关描述于perlrun:,n,l,e
  • 正则表达式可以由YAPE :: Regex :: Explain自动解释

    perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/^\s*(\w+\s+\w+?\s*\([\w0-9,*\s]+\)\s{)/)->explain'
    The regular expression:
    
    (?-imsx:^\s*(\w+\s+\w+?\s*\([\w0-9,*\s]+\)\s{))
    
    matches as follows:
    
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx:                 group, but do not capture (case-sensitive)
                             (with ^ and $ matching normally) (with . not
                             matching \n) (matching whitespace and #
                             normally):
    ----------------------------------------------------------------------
      ^                        the beginning of the string
    ----------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    ----------------------------------------------------------------------
      (                        group and capture to \1:
    ----------------------------------------------------------------------
        \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                 more times (matching the most amount
                                 possible))
    ----------------------------------------------------------------------
        \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                                 or more times (matching the most amount
                                 possible))
    ----------------------------------------------------------------------
        \w+?                     word characters (a-z, A-Z, 0-9, _) (1 or
                                 more times (matching the least amount
                                 possible))
    ----------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                 or more times (matching the most amount
                                 possible))
    ----------------------------------------------------------------------
        \(                       '('
    ----------------------------------------------------------------------
        [\w0-9,*\s]+             any character of: word characters (a-z,
                                 A-Z, 0-9, _), '0' to '9', ',', '*',
                                 whitespace (\n, \r, \t, \f, and " ") (1
                                 or more times (matching the most amount
                                 possible))
    ----------------------------------------------------------------------
        \)                       ')'
    ----------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
    ----------------------------------------------------------------------
        {                        '{'
    ----------------------------------------------------------------------
      )                        end of \1
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------
    
    Run Code Online (Sandbox Code Playgroud)
    • / gm开关在perlre中解释


Tod*_*obs 6

将触发器操作器用于单线器

使用触发器操作符,Perl使这很容易,这将允许您打印出两个正则表达式之间的所有行.例如:

$ perl -ne 'print if /^abcd25/ ... /\bhj \) {/' /tmp/foo
abcd25
ef_gh
( fg*_h
hj_b*
hj ) {
Run Code Online (Sandbox Code Playgroud)

但是,像这样的简单单行将不会区分您想要拒绝分隔模式之间的特定匹配的匹配.这需要更复杂的方法.

更复杂的比较受益于条件分支

单行并不总是最佳选择,如果正则表达式过于复杂,它们可能会很快失控.在这种情况下,你最好编写一个可以使用条件分支的实际程序,而不是尝试使用过于聪明的正则表达式匹配.

一种方法是使用简单模式构建匹配,然后拒绝任何与其他简单模式不匹配的匹配.例如:

#!/usr/bin/perl -nw

# Use flip-flop operator to select matches.
if (/^abcd25/ ... /\bhj \) {/) {
    push @string, $_
};

# Reject multi-line patterns that don't include a particular expression
# between flip-flop delimiters. For example, "( fg" will match, while
# "^fg" won't.
if (/\bhj \) {/) {
    $string = join("", @string);
    undef @string;
    push(@matches, $string) if $string =~ /\( fg/;
};

END {print @matches}
Run Code Online (Sandbox Code Playgroud)

当针对OP的更新语料库运行时,这正确地产生:

abcd25
ef_gh
( fg*_h
hj_b*
hj ) {
abcd25 ef_gh ( fg*_h hj_b* hj ) {
Run Code Online (Sandbox Code Playgroud)

  • @Geekasaur很抱歉,但这与您的语料库和预期输出完全匹配,正如您当前在问题中定义的那样.如果您有其他和/或其他要求,请更新您的问题. (2认同)