Search code examples
regexbackreference

Is it possible to do arithmetics with regular expressions?


I'd like to know whether there is a way to match an integer and its sucessor:

I'd like to match: "1 Victor 2 Marconi"

But not: "1 Victor 3 Marconi"

Is there a way to backreference the first number and increment it like: (\d) [[:alpha:]]* \1 +1 [[:alpha:]]* (INVALID)

I don't know if regexp is the right tool. If not, what would it be ?


Solution

  • You can't do arithmetics with regex. You could possibly create a really big regex which replaces a limited number of numbers by their successor, but not in general.

    When incrementing, there is always the possibility of a carry -> 9+1=10.

    Why do you want to do it with a regex, anyway?