正则表达式中的数学运算 [英] Math operations in regex
本文介绍了正则表达式中的数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在执行替换操作时向反向引用添加一个数字.
I need to add a number to a backreference while doing a replace operation.
例如我正在重写一个 URL
e.g. I am rewriting a URL
www.site.com/doc.asp?doc=321&language=1
到
www.site.com/headline/100321.article
我正在做一个简单的替换,但我需要向文档 ID 添加 100,000.到目前为止,我在下面所做的工作没有添加任何东西.
I'm doing a simple replace, but I need to add 100,000 to the doc ID. What I have below works so far without adding anything.
s/.*doc=(\d+).*/www.site.com\/headline\/$1.article/g;
如何将 100,000 添加到 $1
?
How can I add 100,000 to $1
?
请注意,您不能只在数字前添加 100
,因为文档 ID 可能大于 999.
Note, you can't just add 100
before the number because the doc ID might be > 999.
推荐答案
使用 Perl:
s/.*doc=(\d+).*/"www.site.com\/headline\/".($1+100000).".article"/e;
正如您对 e 标志所做的那样,正确的部分现在变成了一个表达式.所以你必须将非捕获部分包装为字符串.
as you've done with e flag, the right part becomes now an expression. so you have to wrap the non-capture part as strings.
这篇关于正则表达式中的数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文