ANA
ANA

Reputation: 83

Python REGEX: How to add unique random_value at the end of every regex match

Following is my code. It is working but the problem I am having is that it is adding same random value to every Path value. I want to add unique random value at the end of each and every Path value. Any help would be appreciated.

It can be done by json parse but the requirement of the task is to do it via REGEX.

from io import StringIO
import re
import string
import random

reader = StringIO("""{
    "Bounds": [
        {
            "HasClip": true,
            "Lang": "no",
            "Page": 0,
            "Path": "//Document/Sect[2]/Aside/P",
            "Text": "Potsdam, den 9. Juni 2021 ",
            "TextSize": 12.0
        }
    ],
    
},
{
    "Bounds": [
        {
            "HasClip": true,
            "Lang": "de",
            "Page": 0,
            "Path": "//Document/Sect[3]/P[4]",
            "Text": "this is some text ",
            "TextSize": 9.0,
        }
    ],
}""")

def id_generator(size=3, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

text = reader.read()
random_value = id_generator()
pattern = r'"Path": "(.*?)"'
replacement = '"Path": "\\1/'+random_value+'"' 
text = re.sub(pattern, replacement, text)
#This is working but it is only attaching one same random_value on every Path

print(text)

Upvotes: 2

Views: 73

Answers (1)

arutar
arutar

Reputation: 1093

In re.sub you can pass a function which takes a Match object. Then you can expand your match object however you want, including group substitutions.

from io import StringIO
import re
import string
import random

reader = StringIO("""{
    "Bounds": [
        {
            "HasClip": true,
            "Lang": "no",
            "Page": 0,
            "Path": "//Document/Sect[2]/Aside/P",
            "Text": "Potsdam, den 9. Juni 2021 ",
            "TextSize": 12.0
        }
    ],

},
{
    "Bounds": [
        {
            "HasClip": true,
            "Lang": "de",
            "Page": 0,
            "Path": "//Document/Sect[3]/P[4]",
            "Text": "this is some text ",
            "TextSize": 9.0,
        }
    ],
}""")


def id_generator(size=3, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def new_random_group(M):
    return M.expand('"Path": "\\1/' + id_generator() + '"')

text = reader.read()
pattern = r'"Path": "(.*?)"'

text = re.sub(pattern, new_random_group, text)

print(text)

results

{
    "Bounds": [
        {
            "HasClip": true,
            "Lang": "no",
            "Page": 0,
            "Path": "//Document/Sect[2]/Aside/P/R3V",
            "Text": "Potsdam, den 9. Juni 2021 ",
            "TextSize": 12.0
        }
    ],

},
{
    "Bounds": [
        {
            "HasClip": true,
            "Lang": "de",
            "Page": 0,
            "Path": "//Document/Sect[3]/P[4]/HCA",
            "Text": "this is some text ",
            "TextSize": 9.0,
        }
    ],
}

Upvotes: 1

Related Questions