2

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 ?

lilalinux
  • 2,903
  • 3
  • 43
  • 53
Victor Ribeiro
  • 577
  • 7
  • 20

1 Answers1

1

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?

lilalinux
  • 2,903
  • 3
  • 43
  • 53
  • I don't know how to write parsers and my previous experience with regex had been very positive - so I thought it would be feasible. I guess I'll try to create a function that generates "hardcoded" regexs. – Victor Ribeiro Oct 18 '12 at 16:04
  • Not possible with regex. Where do you intend to use the regex? Programming language? Shell script? – lilalinux Oct 18 '12 at 16:07
  • I have a big unstructured file with a numbered list of items. Those items can span into multiple lines. My first attempt was to match anything between numbers. But those items can contain numbers, so it would match an incomplete item. My next idea was to only match items which were between successive numbers. That way I would match the whole item, even if it contained a number. I know that regexps aren't mean to play with unstructured text but I feel this heuristic (a number and its successor) would work well. Yes, I'm using a programming language (Ruby 1.9). – Victor Ribeiro Oct 18 '12 at 16:16
  • Even that wouldn't work, as the item might contain the successor. – lilalinux Oct 18 '12 at 16:21
  • Yes, but I wouldn't mind get few errors. Thanks for your attention. Unfortunately I can't vote up your answer because I don't have the minimum points. But again, thanks! – Victor Ribeiro Oct 18 '12 at 16:30