gpt4 book ai didi

perl - 在perl中增加字符串末尾的整数

转载 作者:行者123 更新时间:2023-12-04 13:29:33 25 4
gpt4 key购买 nike

我有以下格式的字符串:

\main\stream\foo.h\3

它可能有或多或少的“部分”,但总是以斜线结尾,后跟整数。其他例子包括:
\main\stream2309\stream222\foo.c\45

\main\foo.c\9

我需要在 Perl 中增加字符串末尾的数字,其余部分不理会。我在这个网站上找到了一个例子,它完全符合我的要求(见 Increment a number in a string in with regex),只是语言是 Javascript。给出的解决方案是:
.replace(/\d+$/,function(n) { return ++n })

我需要在 Perl 中做同样的事情。

最佳答案

您可以使用 /e regex 修饰符将可执行代码放入替换字符串中。

就像是:

$string =~ s/(\d+)$/$1 + 1/e;

应该管用。

关于perl - 在perl中增加字符串末尾的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3284440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com