Perl - 如何在模式上应用负面的lookbehing / lookahead?

时间:2018-04-27 15:06:25

标签: regex perl

我有实现我的正则表达式的问题。下面是字符串:

my $substitute = q(lalalla = 10..32:1
[thrdddholds]
bbbb = "40..45:1"
cccc = 2..10:5
eqaeaeaea = 5

[bss]
Tad1=3
Nbdfas43p =1..30:1
NbDe524v_1231n= 1);

用模式代替:

if ($substitute =~ /(?:(?:[\d-\.]+[, ]+)+[\d-\.]+)|([-\d+\.]*\.\.[-\d+\.]*:[-\d+\.]*)/) {
  if ($substitute !~ /(")(?:(?:[\d-\.]+[, ]+)+[\d-\.]+)(")/g) {
  $substitute =~ s/([-\d+\.]*\.\.[-\d+\.]*:[-\d+\.]*)/"\1"/g;
  }
}

我的结果

lalalla = "10..32:1"

[thrdddholds]
bbbb = ""40..45:1""
cccc = "2..10:5"
eqaeaeaea = 5

[bss]
Tad1=3
Nbdfas43p ="1..30:1"
NbDe524v_1231n= 1

问题在bbbb = ""40..45:1""行,而""应为",因此固定行应如下所示:bbbb = "40..45:1"

所有预期结果:

lalalla = "10..32:1"

[thrdddholds]
bbbb = "40..45:1"
cccc = "2..10:5"
eqaeaeaea = 5

[bss]
Tad1=3
Nbdfas43p ="1..30:1"
NbDe524v_1231n= 1

如何实现它?我尝试使用负面前瞻和后退,但它只能在一个字符上工作,而不是所有字符串。

1 个答案:

答案 0 :(得分:-2)

试试Regex:

"*(\d+\.\.\d+(?::\d+)*)"*

代替:

"\1"

Demo