0

我在 EditpadLite 中使用了启用 Regex 的 Search&Replace 功能。我的文档看起来像这样

20-10-2011;foo1;foo2;foo3;foo4;foo5
19-10-2011;foo1;foo2;foo3;foo4;
18-10-2011;foo1;foo2;foo3;foo4
17-10-2011;foo1;foo2;foo3;foo4;foo5
16-10-2011;foo1;foo2;foo3;foo4;
15-10-2011;foo1;foo2;foo3;foo4

问题; 每行应包含 4 ;符号,因此第 3 行和第 6 行需要在行尾附加分号,方法是将 \n 替换为 ;\n。我试过了:

(?<!^.*;{3}.*$)\n

选择前面没有正好包含 3 个分号的行的行尾字符。但是,这不起作用,因为我认为分号不是连续的。有替代方案吗?

4

2 回答 2

2
(^(?:[^;]+;){4}[^;]+$) 

应该只匹配第 3 行和第 6 行

只需将匹配替换为$1;

(  //start of group 1
  ^  //start of string
    (  //start of group 2
      ?:  //dont capture matches in group 2
      [^;]+;  //match one or more 'not semicolon' characters followed by a semicolon   
    ){4} //end of group 2, match group 2 exactly 4 times
    [^;]+  //after group 2 matched 4 times there should be one or more 'not semicolon' characters
  $ //end of string
) //end of group 1
于 2011-10-20T11:44:17.050 回答
1

我会使用拆分并计算元素的数量。

这是一种 perl 方法:

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

while(<DATA>) {
    chomp;
    my @l = split /;/;
    $_ .= ";" if @l == 5 && !/;$/;
    print "$_\n";
}

__DATA__
20-10-2011;foo1;foo2;foo3;foo4;foo5
19-10-2011;foo1;foo2;foo3;foo4;
18-10-2011;foo1;foo2;foo3;foo4
17-10-2011;foo1;foo2;foo3;foo4;foo5
16-10-2011;foo1;foo2;foo3;foo4;
15-10-2011;foo1;foo2;foo3;foo4

输出:

20-10-2011;foo1;foo2;foo3;foo4;foo5
19-10-2011;foo1;foo2;foo3;foo4;
18-10-2011;foo1;foo2;foo3;foo4;
17-10-2011;foo1;foo2;foo3;foo4;foo5
16-10-2011;foo1;foo2;foo3;foo4;
15-10-2011;foo1;foo2;foo3;foo4;
于 2011-10-20T11:23:51.013 回答