title: Python Regex: How to match the string and then modify that string by adding something at the end
body: <p>UPDATED CODE: It is working but now the problem is that the code is attaching same random_value to every Path.</p>
  <p>Following is my code with a sample chunk of text. I want to read Path and it's value then add (/some unique random alphabet and number combination) at the end of every Path value without changing the already existed value. For example I want the Path to be like
  &quot;Path&quot; : &quot;already existed value/1A&quot; e.t.c something like that.
  I am unable to make the exact regex pattern of replacing it.
  Any help would be appreciated.</p>
  <p>It can be done by json parse but the requirement of the task is to do it via REGEX.</p>
  <pre><code>from io import StringIO
  import re
  import string
  import random
  
  reader = StringIO(&quot;&quot;&quot;{
      &quot;Bounds&quot;: [
          {
              &quot;HasClip&quot;: true,
              &quot;Lang&quot;: &quot;no&quot;,
              &quot;Page&quot;: 0,
              &quot;Path&quot;: &quot;//Document/Sect[2]/Aside/P&quot;,
              &quot;Text&quot;: &quot;Potsdam, den 9. Juni 2021 &quot;,
              &quot;TextSize&quot;: 12.0
          }
      ],
      
  },
  {
      &quot;Bounds&quot;: [
          {
              &quot;HasClip&quot;: true,
              &quot;Lang&quot;: &quot;de&quot;,
              &quot;Page&quot;: 0,
              &quot;Path&quot;: &quot;//Document/Sect[3]/P[4]&quot;,
              &quot;Text&quot;: &quot;this is some text &quot;,
              &quot;TextSize&quot;: 9.0,
          }
      ],
  }&quot;&quot;&quot;)
  
  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'&quot;Path&quot;: &quot;(.*?)&quot;'
  replacement = '&quot;Path&quot;: &quot;\\1/'+random_value+'&quot;' 
  text = re.sub(pattern, replacement, text)
  #This is working but it is only attaching one same random_value on every Path
  
  print(text)
  </code></pre>