代码之家  ›  专栏  ›  技术社区  ›  con

Negative Lookaward在perl正则表达式中不起作用

  •  1
  • con  · 技术社区  · 4 月前

    我正在解析一个NWChem输出文件,其文本如下:

        General Information
        -------------------
      SCF calculation type: DFT
      Wavefunction type:  closed shell.
      No. of atoms     :    10
      No. of electrons :    36
       Alpha electrons :    18
        Beta electrons :    18
      Charge           :     0
      Spin multiplicity:     1
      Use of symmetry is: on ; symmetry adaption is: on 
      Maximum number of iterations:  30
      AO basis - number of functions:    95
                 number of shells:    45
      Convergence on energy requested:  1.00D-06
      Convergence on density requested:  1.00D-05
      Convergence on gradient requested:  5.00D-04
    
          XC Information
          --------------
    

    我已将文件保存为字符串 $str ,并将每个换行符替换为 я . 上述文本在文件中出现了大约10次,所以我想用这样的东西来捕获它们 General Information :

    my @capture = $str =~ m/General\s+Informationя
    \s+[-]+я
    (.+(?!\-{2,})) # negative lookahead, no more than 2 "-" characters
    яя\s+[-]+
    /xg;
    

    上面的正则表达式只抓取了整个文件,这是不正确的。

    我也试过了 (.+(?![\-]{2,})) 哪一个 捕获的文本比它应该捕获的要多得多。

    如何更改正则表达式 (.+(?!\-{2,})) 因此不超过2 - 里面允许有字符吗?

    2 回复  |  直到 4 月前
        1
  •  2
  •   ikegami Gilles Quénot    4 月前

    只捕捉 General Information 部分,

    my $gi = /
       ^
       \s* General[ ]Information \n  # A line with the header
       \s* -{2,} \n                  # Followed by a separator line.
       (?: .* \n (?! \s* -- ) )*     # Lines not followed by a separator.
    /xm ? $& : undef;
    

    为了分别捕获每个部分,

    my @sections = /
       ^
       \s* \S[\S\h]* \n              # A line with the header.
       \s* -{2,} \n                  # Followed by a separator line.
       (?: .* \n (?! \s* -- ) )*     # Lines not followed by a separator.
    /xmg;
    
        2
  •  0
  •   aaa    4 月前

    虽然这是可能的,但你可能可以在不使用负面前瞻的情况下使用以下内容来捕捉它:

    \s*General\s+Information\s+(?:---)+-*[\s\S]+?(?:---)+-*\s*$
    

    You can read the details here