Mayday
Mayday

Reputation: 5146

Python regex replace digit -1

I am trying to convert a String into proper JSON notation. This string, got some correct indexes (with [idx]), and some incorrect indexes (with dot notation .idx. with these last ones starting by 1, instead of by 0). Is there anyway to "handle" captured groups using python re library or similar?

This is what I have:

import re

a = "a.[0].b.1.c"
re.sub(r'\.(\d)\.', r'.[\1].', a)  # Which provides: 'a.[0].b.[1].c'

I would like it to give me 'a.[0].b.[0].c', but I do not know how can I perform operations on the captured group $1. Is it possible?

Upvotes: 2

Views: 99

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18306

The replacer argument of re.sub can be a function and that function gets passed the match object upon which you can perform operations:

def replacer(mo):
    captured_group = mo.group(1)
    one_subtracted = str(int(captured_group) - 1)
    to_replace = ".[" + one_subtracted + "]."
    return to_replace

and using:

>>> re.sub(r"\.(\d)\.", replacer, a)
'a.[0].b.[0].c'

Note that mo includes all the to-be-replaced string that your pattern matches, so we put the matched .s back.

If you want a lambda:

re.sub(r"\.(\d)\.", lambda mo: ".[" + str(int(mo.group(1))-1) + "].", a)

Upvotes: 2

Related Questions