2

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?

2

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

You just missed to add the square brackets. Would be ".[" + ... + "].". Appart from that, ir works like a charm! Thank you very much!!!
@Mayday Yeah, i was editing that :) Glad to be of help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.