我想替换字符串中第二次出现的数字 [英] I want to replace the second occurrence of the number in the string
本文介绍了我想替换字符串中第二次出现的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串说一个像下面这样的网址
I have a string say a url like below
"www.regexperl.com/1234/34/firstpage/home.php"
现在我需要用 2 替换字符串中第二次出现的数字 34.
Now i need to replace the 34 number that is the second occurrence of a number in the string with 2.
结果字符串应该像
"www.regexperl.com/1234/2/firstpage/home.php"
我面临的挑战是当我尝试存储值 34 并替换它时,它正在替换数字 1234 中的 34 并给出如下结果
The challenge I m facing is when i try to store the value 34 and replace it , It is replacing the 34 in the number 1234 and gives the result like below
"www.regexperl.com/122/34/firstpage/home.php"
请告诉我一个合适的正则表达式来解决问题.
Kindly let me know a proper regex to solve the problem.
推荐答案
使用 \K
.
^.*?\d+\b.*?\K\d+
替换为你的字符串
.查看演示.
https://regex101.com/r/lW2kK1/1
这篇关于我想替换字符串中第二次出现的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文