使用lt(<)时,REGEX错误中的PERL'e'(eval)

时间:2016-07-20 22:26:49

标签: regex perl

我在命令行中使用一个内联来进行REGEX替换。这有效:

perl -pe 's/<span class="num">(\d\d?)/<span class="num">+$1 + 1/eg' calendar1.html  > output.html

但是当我在开头添加<td>时,我收到错误:

perl -pe 's/<td><h3 class="day"><span class="num">(\d\d?)/<td><h3 class="day"><span class="num"> +$1 + 1/eg' calendar1.html  > output.html

错误:

Can't modify numeric lt (<) in scalar assignment at -e line 1, near "1}"

我想要匹配的html结构如下所示:

<td>
      <h3 class="day"><span class="num">10

我正在尝试1到每个一位或两位数

2 个答案:

答案 0 :(得分:0)

使用/e,替换必须是Perl表达式。

My Perl(5.20.1)抱怨<span class="num">$1 + 1,我认为这是对的:那不是表达。在需要的地方添加引号和连接:

s/<span class="num">+(\d)/q(<span class="num">) . ($1 + 1)/eg

同样,对于第二个表达式:

s/<td><h3 class="day"><span class="num">(\d\d?)/q(<td><h3 class="day"><span class="num">) . ($1 + 1)/eg

或更短

s/(<td><h3 class="day"><span class="num">)(\d\d?)/$1 . ($2 + 1)/eg

答案 1 :(得分:0)

choroba把我的答案给了我:/ e表达了;尝试这样的事情:

echo '<td> <h3 class="day"><span class="num">10' | perl -pe 's/<td>\s*<h3\s+class="day"><span\s+class="num">(\d\d?)/"<td><h3 class=\"day\"><span class=\"num\">".($1+1)/ge'