pythonregex

Replace a text line based on a pattern in previous text line using regular expression in Python


Requirement: Replace the text line occurrence of "url: http://some.web.com/GVH-JBoss.ear" based on a text in previous line i.e "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.


Solution

  • 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