如何使用正则表达式将字符串“a b a c d a a”转换为字符串“1 b 2 c d 3 4”?
这可能吗?首选的口味是 perl,但任何其他口味也可以。
s/a/ \????? /g
最佳答案
这种替代将做到这一点。
$ perl -p -e 's/a/++$i/ge'
a b a c d a a
1 b 2 c d 3 4
关于regex - 正则表达式 : replace match with its index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211023/