1

It is easy to convert 11 12 13 14 to 11a 12a 13a 14a, using:

myStr = "11 12 13 14"
myStr = re.sub(r'(\d*)', r'\1a', myStr)
print myStr # 11a 12a 13a 14a

but how can I turn 11 12 13 14 to 12 13 14 15 by using re.sub?

miradulo
  • 28,857
  • 6
  • 80
  • 93
playSCforever
  • 266
  • 1
  • 9
  • 3
    Is `re.sub` a requirement? Regex substitution isn't generally suited for things like incrementing numbers. – miradulo Jun 24 '16 at 07:48
  • 3
    You can use a [**callback for re.sub**](http://stackoverflow.com/questions/2094975/python-re-sub-question) – Jan Jun 24 '16 at 07:51
  • 3
    `' '.join([str(int(s) + 1) for s in myStr.split()])` would be much easier (and considerably more readable) than faffing around with regular expressions. – jonrsharpe Jun 24 '16 at 07:51
  • I don't think that it is possible in general. This would require manipulating numbers (adding one to each of them), but regular expressions don't know about numbers (they know about symbols used to write them - 0123..., but don't know that they are different than say abcd...) – Matthew Jun 24 '16 at 07:51
  • 2
    @jonrsharpe Off topic: many thanks for introducing me to the word _faffing_. – miradulo Jun 24 '16 at 07:52
  • Related : [Is it possible to increment numbers using regex substitution?](http://stackoverflow.com/questions/12941362/is-it-possible-to-increment-numbers-using-regex-substitution) – miradulo Jun 24 '16 at 07:55
  • @Matthew: See answer below, of course it is possible. – Jan Jun 24 '16 at 08:33
  • This [*Incrementing the last digit in a Python string*](http://stackoverflow.com/questions/23820883/incrementing-the-last-digit-in-a-python-string) is very close. – Wiktor Stribiżew Jun 24 '16 at 08:35
  • 1
    @Jan I stand corrected. It isn't possible to do with regular expressions on their own, as I stated, but I wasn't aware that Python allowed a callback function to be passed to the sub function like that. – Matthew Jun 24 '16 at 10:11
  • @Matthew: Right you are. – Jan Jun 24 '16 at 10:50

2 Answers2

5

A regex solution with re.sub():

import re
string = "11 12 13 14"

def repl(m):
    number = int(m.group(1)) + 1
    return str(number)

print re.sub(r'\b(\d+)\b', repl, string)
# 12 13 14 15

See a demo on ideone.com.

As others mentioned, this might not be the most appropriate solution though.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Jan
  • 42,290
  • 8
  • 54
  • 79
2

The simplest approach for a string that only contains integer numbers separated with space is

s = "11 12 13 14"
print(" ".join([str(int(x)+1) for x in s.split()]))
# => 12 13 14 15

See the IDEONE demo

An alternative with a re.sub can be used with the help of a \d+ regex pattern and a lambda in the replacement part:

import re
s = "11 12 13 14"
res = re.sub(r'\d+', lambda x: str(int(x.group()) + 1), s)
print(res) # => 12 13 14 15

Another IDEONE demo

The advantage of the regex approach is:

  • You can increment number inside larger texts containing words, punctuation, etc.
  • You can enchance/further precise the pattern to only increment number in specific context (like, only increment numbers followed with $ with \d+(?=\$) or that are not part of float values with (?<!\d\.)\b\d+\b(?!\.\d))
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563