是否可以使用正则表达式替换来增加数字? [英] Is it possible to increment numbers using regex substitution?

查看:38
本文介绍了是否可以使用正则表达式替换来增加数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用正则表达式替换来增加数字?不使用 评估/基于函数的替换,当然.

Is it possible to increment numbers using regex substitution? Not using evaluated/function-based substitution, of course.

这个问题的灵感来自另一个问题,提问者想要在文本编辑器中增加数字.支持正则表达式替换的文本编辑器可能比支持完整脚本的文本编辑器更多,因此如果存在正则表达式,则可能更方便浮动.

This question was inspired by another one, where the asker wanted to increment numbers in a text editor. There are probably more text editors that support regex substitution than ones that support full-on scripting, so a regex might be convenient to float around, if one exists.

此外,我经常从聪明的解决方案中学到一些巧妙的东西,解决几乎没有用的问题,所以我很好奇.

Also, often I've learned neat things from clever solutions to practically useless problems, so I'm curious.

假设我们只讨论非负十进制整数,即 \d+.

  • 是否可以单次替换?或者,有限数量的替换?

  • Is it possible in a single substitution? Or, a finite number of substitutions?

如果不是,是否至少有可能给定一个上限,例如数字高达 9999?

If not, is it at least possible given an upper bound, e.g. numbers up to 9999?

当然,如果有一个 while 循环(替换 while 匹配),它当然是可行的,但我们在这里寻求一个无循环的解决方案.

Of course it's doable given a while-loop (substituting while matched), but we're going for a loopless solution here.

推荐答案

这个问题的主题让我很高兴,因为我之前做过一个特定的实现.我的解决方案恰好是两个替换,所以我会发布它.

This question's topic amused me for one particular implementation I did earlier. My solution happens to be two substitutions so I'll post it.

我的实现环境是solaris,完整示例:

My implementation environment is solaris, full example:

echo "0 1 2 3 7 8 9 10 19 99 109 199 909 999 1099 1909" |
perl -pe 's/\b([0-9]+)\b/0$1~01234567890/g' |
perl -pe 's/\b0(?!9*~)|([0-9])(?=9*~[0-9]*?\1([0-9]))|~[0-9]*/$2/g'

1 2 3 4 8 9 10 11 20 100 110 200 910 1000 1100 1910

拆开解释:

s/\b([0-9]+)\b/0$1~01234567890/g

对于每个数字 (#) 将其替换为 0#~01234567890.第一个 0 是在需要将 9 舍入到 10 的情况下.01234567890 块用于递增.9 10"的示例文本是:

For each number (#) replace it with 0#~01234567890. The first 0 is in case rounding 9 to 10 is needed. The 01234567890 block is for incrementing. The example text for "9 10" is:

09~01234567890 010~01234567890

下一个正则表达式的各个部分可以单独描述,它们通过管道连接以减少替换次数:

The individual pieces of the next regex can be described seperately, they are joined via pipes to reduce substitution count:

s/\b0(?!9*~)/$2/g

选择所有不需要四舍五入的数字前面的0"位并舍弃.

Select the "0" digit in front of all numbers that do not need rounding and discard it.

s/([0-9])(?=9*~[0-9]*?\1([0-9]))/$2/g

(?=) 是正向前瞻,\1 是匹配组 #1.因此,这意味着匹配所有后跟 9 的数字,直到~"标记,然后转到查找表并找到此数字后面的数字.替换为查找表中的下一个数字.因此,当正则表达式引擎解析数字时,09~"变为19~"然后是10~".

(?=) is positive lookahead, \1 is match group #1. So this means match all digits that are followed by 9s until the '~' mark then go to the lookup table and find the digit following this number. Replace with the next digit in the lookup table. Thus "09~" becomes "19~" then "10~" as the regex engine parses the number.

s/~[0-9]*/$2/g

这个正则表达式删除 ~ 查找表.

This regex deletes the ~ lookup table.

这篇关于是否可以使用正则表达式替换来增加数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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