Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我的标量:
my $var = "foo1";
这是我试图做的:
$var =~ s/[0-9]/_[0-9]/;
结果:
foo_[0-9]
预期结果:
foo_1
如果您能回答foo在比赛后查找和添加下划线,以及1在之前查找和添加下划线,那就太好了。
foo
1
$var = s/foo/something/
和
$var = s/[0-9]/something/
提前致谢
捕获您要插入的内容,然后插入您捕获的内容:
s/([0-9])/_$1/;
或使用前瞻:
s/(?=[0-9])/_/;
根据要求,也匹配的解决方案foo:
s/(foo)([0-9])/$1_$2/; s/foo\K([0-9])/_$1/; s/foo\K(?=[0-9])/_/;
请注意,捕获会减慢匹配速度。