2

i have a regex substitution that i cannot figure out.

s2 = re.compile("^DATA_ARRAY (.*?};)", re.DOTALL|re.MULTILINE)    
result = re.sub(s2, "newData", inputData)

The problem is that it replaces the "DATA_ARRAY" part as well. I need to replace only the text that comes AFTER the "DATA_ARRAY" part. I have placed the data in a group in the s2 (.*?};)but cannot figure out how to use the re.sub function to replace only the required part.

Regards

fedorqui
  • 275,237
  • 103
  • 548
  • 598
ele lont
  • 469
  • 1
  • 4
  • 11

2 Answers2

2
s2 = re.compile("^(DATA_ARRAY )(.*?};)", re.DOTALL|re.MULTILINE)    
result = re.sub(s2, r"\1newData", inputData)

You can capture the first group and replace via backreferencing

or simply

s2 = re.compile("^DATA_ARRAY (.*?};)", re.DOTALL|re.MULTILINE)    
result = re.sub(s2, "DATA_ARRAY newData", inputData)
vks
  • 67,027
  • 10
  • 91
  • 124
  • Thanks, this helped me on the right track! I actually had to use a named backreference since the "newData" was in a variable. – ele lont May 27 '15 at 16:22
0
s2 = re.compile("(?<=^DATA_ARRAY )(.*?};)", re.DOTALL|re.MULTILINE)    
result = re.sub(s2, "newData", inputData)

You can just have a lookbehind assertion (?<=___).

karthik manchala
  • 13,492
  • 1
  • 31
  • 55