简体   繁体   English

Perl Shell脚本转义的替换模式不起作用

[英]perl shell script escaped replace pattern not working

I have this NOT working issue. 我有这个行不通的问题。

echo "aabbccdd" | perl -w -pe "s/(?<Naa>aa)/\g{Naa}-$1/;"

it outputs 它输出

Unrecognized escape \\g passed through at -e line 1. 无法识别的转义\\ g通过-e行1。

my perl version is "subversion 4 (v5.18.4)" 我的perl版本是“ subversion 4(v5.18.4)”

my bash version is "version 4.2.53(1)-release (x86_64-redhat-linux-gnu)" 我的bash版本是“版本4.2.53(1)-发行版(x86_64-redhat-linux-gnu)”

my OS distro is "Fedora release 20 (Heisenbug)" 我的操作系统发行版是“ Fedora版本20(Heisenbug)”

my locale is "C" 我的语言环境是“ C”

how to solve ? 怎么解决 ?

update: 更新:

how to display same labeled group by number 1 ? 如何显示编号为1的相同标签组?

You can only use \\g{name} (or \\k<name> ) on the matching side of the regular expression. 您只能在正则表达式的匹配侧使用\\g{name} (或\\k<name> )。 You can use $+{name} on the replacement side: 您可以在替换端使用$+{name}

echo "aabbaaccdd" | perl -w -pe "s/(?<Naa>aa)(?=bb\g{Naa}).*/$+{Naa}/;"

prints out 打印出来

aa

This is because named captures are placed in the hash table %+ . 这是因为命名捕获放置在哈希表%+

EDIT: Wiktor Stribiżew beat me to it while I was writing this, in the comments to the question. 编辑:WiktorStribiżew在写这篇文章时在问题的评论中击败了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM