【问题标题】:Perl Regex segregating text chunksPerl 正则表达式分离文本块
【发布时间】:2014-05-15 08:43:32
【问题描述】:

我有一个长文本文件,例如:

COMPONENT;SCALAR;VALUE
point1/point2 - Value Value Value
point1/point2 - Value Value Value
point1/point2 - Value Value Value
point1/point2 - Value Value Value
point1/point2 - Value Value Value
point1/point2 - Value Value Value
point1/point2 - Value Value Value

COMPONENT;SCALAR;VALUE
point3/point4 - Value Value Value
point3/point4 - Value Value Value
point3/point4 - Value Value Value
point3/point4 - Value Value Value
point3/point4 - Value Value Value
point3/point4 - Value Value Value
point3/point4 - Value Value Value

现在我已经提取了 'point3/point4' 类型值的所有唯一值 .. 现在我要做的是提取整个数据块 .. 例如 From COMPONENT;SCALAR ----> \n \n (在块的末尾有两个新行)。现在我想做的是

string =~ /(COMPO\S\spoint1\/point2)(.+?)(COMP)/s;
print $2;

但它不起作用。匹配应该只匹配到第一个 '\n\n' 并且不要贪婪地捕获最大值。任何一个班轮将不胜感激。

【问题讨论】:

    标签: regex perl


    【解决方案1】:

    使用 perl 单行。

    perl -00 -ne 'print if m{point1/point2};' data.txt
    

    唯一的窍门是从perlrun 使用-00 进行段落模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多