Perl:如何在对变量进行分组后插入数字? [英] Perl: How do you insert numbers after grouping variable?

查看:59
本文介绍了Perl:如何在对变量进行分组后插入数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Perl one liner 中有以下替换:

I've got the following substitution in a Perl one liner:

perl -pi.bak -e 's/(.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t)/$123424977\t/g if $. <= 200'

问题是我想在分组正则表达式 (.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t).

The problem is that I want to insert the number 23424977 after the text that is captured by the grouped regex (.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t).

但是 Perl 认为我指的是 $123424977 组!我如何告诉 Perl 我想要组 $1 并在此之后插入文本 23424977?

But Perl thinks I'm referring to group $123424977! How do I tell Perl I want group $1 and to insert the text 23424977 after that?

推荐答案

您可以使用花括号明确地分隔变量名 —${1} 而不是 $1:

You can use curly braces to unambiguously delimit the variable-name — ${1} instead of $1:

perl -pi.bak -e 's/(.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t)/${1}23424977\t/g if $. <= 200'

这篇关于Perl:如何在对变量进行分组后插入数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆