python使用正则表达式搜索和更新字符串 [英] python to search and update string with regex

查看:29
本文介绍了python使用正则表达式搜索和更新字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的字符串,我能够获取我想要的 'text' (文本在模式之间扭曲).代码如下,

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)

我想用用户提供的新值/或通过读取另一个 XML 来替换值(Text1、text2、tex3 等).(Text1, tex2 .. 是完全随机的字母数字数据.下面是一些细节

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"}]

我决定使用 replace() 但后来意识到数据不是恒定的.因此再次寻求帮助.感谢您的回复.

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.

推荐答案

您可以通过将 backreferences 与 re.sub 结合使用来做到这一点:

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> 是第一个 () 中的文本.

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

编辑

也许更好的方法是解码字符串,更改数据并再次对其进行编码.这应该能让您更轻松地访问这些值.

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)

我希望这会有所帮助.

这篇关于python使用正则表达式搜索和更新字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆