【问题标题】:python to search and update string with regexpython使用正则表达式搜索和更新字符串
【发布时间】:2017-10-16 14:27:28
【问题描述】:

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

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 .. 是完全随机的字母数字数据。下面是一些细节

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(),但后来意识到数据不是恒定的。因此再次寻求帮助。感谢您的回复。

任何帮助将不胜感激。另外,如果让我知道我是否可以改进我现在获取价值的方式,因为我是正则表达式的新手。

【问题讨论】:

  • anstring 是预期结果
  • 所以它就像 JSON,带有键值的对象数组,您尝试替换值。我说的对吗?
  • 字符串 text1,text2,text3 等你以前知道吗?或者它只是基于它们放置位置的这种模式?如果它们是已知的,那么只需创建一个字典并将这些值映射到 newText 将被替换并替换为所有使用 .replace() 如果值已知,您甚至可能不需要在这里
  • @MykolaShchetinin:是的,它是键值对,我正在尝试替换值。
  • @MohitC :我从 XML 中读取了这个字符串。我必须用可用的新值(来自用户输入(YML)或另一个 XML)更新 Text1、text2、tex3,一旦更新字符串,我需要将此字符串写回原始 XML

标签: python regex string python-2.7


【解决方案1】:

您可以将backreferences 与 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&lt;1&gt; 是第一个() 中的文本。

编辑

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

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)

我希望这会有所帮助。

【讨论】:

  • 我已经通过使用这个来工作:ansstring = re.sub(r'(?&lt;=:&amp;quot;)([^(]*)', r'new\g&lt;1&gt;' , val1)
  • 因为你的脚本也替换了引号
  • @JanZeiseweis:感谢您的快速帮助..!
  • @JanZeiseweis :为之前的困惑道歉,我已经用更多信息更新了我的问题。 replace() 对我不起作用,re.sub(r'(?&lt;=:&amp;quot;)([^(]*)', r'new\g&lt;1&gt;' , val1)new 附加到现有值。我想用新值更新值(我正在从单独的 XML 文件中读取这些值,我不知道)
  • @JanZeiseweis:这就是我要找的。我正在研究 Python 2.7,遗憾的是 html 在 python 2.7 中没有转义和未转义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 2011-07-28
相关资源
最近更新 更多