Math operations in regex

后端 未结 3 1273
予麋鹿
予麋鹿 2020-11-27 06:58

I need to add a number to a backreference while doing a replace operation.

e.g. I am rewriting a URL

www.site.com/doc.asp?doc=321&language=1
         


        
3条回答
  •  执念已碎
    2020-11-27 07:20

    That's not possible in regex. Regex only matches patterns, it doesn't do arithmetic.

    The best you can do is something verbose like:

    match       replace
    
    (\d{6,})    $1
    (\d{5})     1$1
    (\d{4})     10$1
    (\d{3})     100$1
    (\d{2})     1000$1
    (\d)        10000$1
    

提交回复
热议问题