2

I have below string, I am able to grab the 'text' what I wanted to (text is warped between pattern). code is give below,

val1 = '[{"vmdId":"Text1","vmdVersion":"text2","vmId":"text3"},{"vmId":"text4","vmVersion":"text5","vmId":"text6"}]'


temp = val1.split(',')
list_len =  len(temp)

for i in range(0, list_len):
    var = temp[i]
    found = re.findall(r':"([^(]*)\&quot\;', var)
    print ''.join(found)

I would like to replace values (Text1, text2, tex3, etc) with new values provided by user / or by reading from another XML. (Text1, tex2 .. are is totally random and alphanumeric data. below some details

Text1 = somename
text2 = alphanumatic value
text3 = somename

Text4 = somename
text5 = alphanumatic value
text6 = somename

    anstring =
 [{"vmdId":"newText1","vmdVersion":"newtext2","vmId":"newtext3"},{"vmId":"newtext4","vmVersion":"newtext5","vmId":"newtext6"}]

I decided to go with replace() but later realize data is not constant. hence seeking for help again. Appreciate your response.

Any help would be appreciated. Also, if let me know if I can improve the way i am grabing the value right now, as i new with regex.

7
  • anstring is the expected result Commented Oct 16, 2017 at 14:32
  • 1
    so it is like JSON, array of objects with key-values, you try to replace values. Am I right? Commented Oct 16, 2017 at 14:32
  • 1
    are the strings text1,text2,text3 etc known to you from before? Or its just based on this pattern of where they are placed? If they are known then simply create a dictionary and map these values to newText which is to be replaced and replace for all using .replace() You possible dont even need re here if values are known Commented Oct 16, 2017 at 14:33
  • @MykolaShchetinin: yes, it's key-value pair, and I am trying to replace values. Commented Oct 16, 2017 at 15:53
  • @MohitC : I have read this string from XML. and I have to update Text1, text2, tex3 with new values available (either from user input (YML) or another XML), Once string is update, I need to write this string back to original XML Commented Oct 16, 2017 at 15:57

1 Answer 1

2

You can do this by using backreferences in combination with re.sub:

import re
val1 = '[{"vmdId":"Text1","vmdVersion":"text2","vmId":"text3"},{"vmId":"text4","vmVersion":"text5","vmId":"text6"}]'

ansstring = re.sub(r'(?<=:&quot;)([^(]*)', r'new\g<1>' , val1)

print ansstring

\g<1> is the text which is in the first ().

EDIT

Maybe a better approach would be to decode the string, change the data and encode it again. This should allow you to easier access the values.

import sys

# python2 version
if sys.version_info[0] < 3:
    import HTMLParser
    html = HTMLParser.HTMLParser()
    html_escape_table = {
        "&": "&amp;",
        '"': "&quot;",
        "'": "&apos;",
        ">": "&gt;",
        "<": "&lt;",
        }

    def html_escape(text):
        """Produce entities within text."""
        return "".join(html_escape_table.get(c,c) for c in text)

    html.escape = html_escape
else:
    import html

import json

val1 = '[{&quot;vmdId&quot;:&quot;Text1&quot;,&quot;vmdVersion&quot;:&quot;text2&quot;,&quot;vmId&quot;:&quot;text3&quot;},{&quot;vmId&quot;:&quot;text4&quot;,&quot;vmVersion&quot;:&quot;text5&quot;,&quot;vmId&quot;:&quot;text6&quot;}]'
print(val1)

unescaped = html.unescape(val1)
json_data = json.loads(unescaped)
for d in json_data:
    d['vmId'] = 'new value'

new_unescaped = json.dumps(json_data)
new_val = html.escape(new_unescaped)
print(new_val)

I hope this helps.

Sign up to request clarification or add additional context in comments.

7 Comments

I have got this to work by using this: ansstring = re.sub(r'(?<=:&quot;)([^(]*)', r'new\g<1>' , val1)
because your script was also replacing quotes
@JanZeiseweis: Appreciate for your quick help..!
@JanZeiseweis : Apologies for earlier confusion, I have updated my question with more info. replace() is not working for me, and the re.sub(r'(?<=:&quot;)([^(]*)', r'new\g<1>' , val1) is appending new to the existing value. I would like to update value with new value (I am reading that values from separate XML file, and not known to me)
@JanZeiseweis: This is what I am looking for. I am working on Python 2.7 and sadly html dont have escape and unescaped in for python 2.7
|

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.