Requirement: Replace the text line occurrence of "url: http://some.web.com/GVH-JBoss.ear " based on a text in previous line ie "GVH:" with a new text [say: url: ftp://new.web.com/new.ear] .
Example: Consider text lines as below: GVH: url: http://some.web.com/GVH-JBoss.ear sha1: 7b7b797735822d411c288d14510e9e023001d3ae VID: url: http://some.web.com/VID.ear sha1: 2fcac8bdcfadcfc12f0a7dfef0bad01db5f8f8a8
Expected: GVH: url: ftp://new.web.com/new.ear sha1: 7b7b797735822d411c288d14510e9e023001d3ae VID: url: http://some.web.com/VID.ear sha1: 2fcac8bdcfadcfc12f0a7dfef0bad01db5f8f8a8
I tried using python regex [re.sub() method] to achieve this:
re.sub(r'\\s+GVH:[\\s]*\\s+url:\\s\\w+.*ear', 'url: ftp://new.web.com/new.ear', line.rstrip(), re.MULTILINE)
Other Regexes tried to match this specified pattern: 1. \\s+GVH:[\\s]*\\s+url:\\s\\w+.*ear 2. (\\s+GVH:\\n)?\\s*url:\\s+\\w+.*ear$ 3. (\\s+GVH:\\n)?\\s*url:\\s+\\w+.*ear$ 4. [(?<=GVH:\\s).*url:\\s\\w+.*ear$] 5. (?<=\\sGVH:[\\s\\S])url: \\w+.*ear 6. [\\s]GVH:[\\s\\S](?=(\\s+url: [\\w]\\.ear) 7. (^.*GVH:[\\s]?$)|(^.*url:\\s\\w+.*ear$)
With all these regexes, Was able to find the text of either of the lines only but not both.
All of them failed to capture and replace those lines of text.
Need help in this regard.
print (re.sub(r'(GVH:\s+url:\s+).*?ear', r'\1ftp://new.web.com/new.ear', line))
GVH:
url: ftp://new.web.com/new.ear
sha1: 7b7b797735822d411c288d14510e9e023001d3ae
HVA:
url: http://some.web.com/HVA-JBoss.ear
sha1: e3ec053c65af5ce134c469ebbe3d7da29995369f
You can use the yaml
module.
Ex:
import yaml
with open(filename) as f:
data = yaml.load(f) #Read yml file
newVal = "ftp://new.web.com/new.ear"
data["GVH"]["url"] = newVal #Update Value
with open(filename, 'w') as outfile:
yaml.dump(data, outfile, default_flow_style=False) #Write Back
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.