0

Add number to numbers inside a text python:

Input: Sono stata 21 commossa che lei ci ha 45 letto

Output: Sono stata 22 commossa che lei ci ha 46 letto

My code: text_after = re.sub("^[0-9]+", "^[0-9]+"+1, text)

3
  • can you post all your code please? Commented Mar 27, 2022 at 21:55
  • 1
    You cannot use regular expressions to do mathematical operations. You could use it to extract the number from the text to a variable, add +1, then replace the number with regex Commented Mar 27, 2022 at 21:56
  • 2
    Just use a re.sub with a callable as replacement argument. re.sub(r'\d+', lambda x: str(int(x.group())+1), s). See ideone.com/dX1C96 Commented Mar 27, 2022 at 21:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.