2

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)
1
  • It only assigns one random value because you only ever generate one random value. Assigning the returned string from theid_generator() function to random_value and then using random_value in replacement doesn't cause the function to be called and a new value to be generated every time replacement is used. Commented Jan 31, 2022 at 15:25

1 Answer 1

1

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

Comments

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.