我正在尝试将注释字符插入类似于以下内容的字符串中:
-CreateVideoTracker VT1 "vt name"
成为
-CreateVideoTracker VT1 # "vt name"
VT1 词实际上可以是任何东西,所以我使用的是正则表达式
$line =~ s/\-CreateVideoTracker \w/\-CreateVideoTracker \w # /g;
这给了我结果:
-CreateVideoTracker w #T1 "vt name"
有什么方法可以用一个正则表达式来做到这一点,还是我需要拆分字符串并手动插入注释?
最佳答案
$line =~ s/^(\-CreateVideoTracker)\s+(\w+)/$1 $2 #/;
正则表达式前半部分中括号中的表达式(称为“捕获缓冲区”)被引用为 $1
、$2
。下半场等等。
关于regex - 如何使用 s///运算符将字符插入到字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/284722/