如何从下面的行读取字符串?

时间:2013-09-13 13:20:57

标签: regex perl

我有这个:

//VIOLATION_IGNORED
T_CH  *i_pCH2string;

我想从第二行读取// VIOLATION_IGNORED。我怎样才能使用正则表达式进行处理?

我是用Understand API写的。理解是一个静态分析工具,通过perl使用它自己的API来编写脚本。我只需要一个正则表达式来读取第一行,从第二行开始。

2 个答案:

答案 0 :(得分:1)

你没有阅读上一行...你只记得上面的内容。 伪代码:

while(<>) {
   if ((/\/\/VIOLATION_IGNORED/) {$ignore=1;next;} # ignore violation on next line
   if (violation($_)) {                            # yo' bad?
       if($ignore) {ignore=0; next;}               # never mind
    } else {
       blow_up($_);                                # take this!
    }
   ignore=0;                                       # reset flag
}

答案 1 :(得分:0)

这会实现您的目标吗?

#!/usr/bin/perl
use warnings;
use strict;

my @file = ('//VIOLATION_IGNORED', 'T_CH  *i_pCH2string');

my $current_line = "";

foreach (@file){
    my $previous_line = $current_line;
    $current_line = $_;
        if ($current_line =~ /T_CH.*?/ ){
            print "$previous_line\n"
            # Do something else? ...
        }
}

输出:

//VIOLATION_IGNORED

或者,如果您只是想忽略包含// VIOLATION_IGNORED:

的行
foreach (@array1){
    next if $_ =~ /\/\/VIOLATION_IGNORED/;
    print "$_\n";
    # Do something else? ...
}

输出:

T_CH  *i_pCH2string
相关问题