title: If string is preceded by pattern of numbers and colons
body: <p>I'm pretty new to Python and I've been trying to work out how to use if statements where it looks if a particular string is preceded by a certain pattern. </p>
  
  <p>For example, the text in particular</p>
  
  <p><code>Sep 09 07:54:28 INFO: line of text here</code></p>
  
  <p>I have multiple lines like this in a file that I have my script reading from. The dates and times change on every line so I can't specify that text exactly.</p>
  
  <p>I'm trying to replace the word <code>INFO</code> with something else.</p>
  
  <p>However, the word <code>INFO</code> is scattered throughout the text file and I don't want to replace every instance of it.</p>
  
  <p>I only want to replace <code>INFO</code> if it's preceded by <code>number number, colon, number number, colon, number number</code>.</p>
  
  <p>So I've used <code>if</code> statements, <code>string.replace(old, new)</code> and I've been reading about 'positive lookbehind assertions' eg. <code>(?&lt;=abc)def</code></p>
  
  <p>But I'm unsure how to specify a pattern of text, rather than specifying the exact text.</p>
  
  <p>Just need pointing in the right direction! Thanks</p>
  
  <p>EDIT: I should also point out that there are other instances of <code>INFO</code> that is preceded by a number, so I didn't want to make the rule simply 'be preceded by a number'. It will need to be specifically that pattern (xx:xx:xx)</p>
  
  <p>EDIT2: Putting another example here to clarify further based on a comment</p>
  
  <pre><code>Sep 09 07:54:28 INFO: line of text here that contains many words
  
  line of text that also contains the word INFO in the same line
  
  Sep 09 07:56:30 INFO: line of text here that also contains many words
  
  121334234: line of text here that contains INFO as well
  </code></pre>
  
  <p>I want replace the word INFO, but only on lines that have the time in that format (num, num, colon, num num, colon, num num)</p>
  
  <p>EDIT 3:</p>
  
  <pre><code>with open(infile) as f:
      f = f.read()
  
  with open(infile, 'r') as IN, open('output.html', 'w') as OUT:
      f = re.sub(r'(?&lt;=\d{2}:\d{2}:\d{2})\s*INFO\b', ' INFO2', f)
  </code></pre>
  
  <p>this isn't returning any error but it doesn't perform any action</p>
  
  <p>EDIT 4:</p>
  
  <pre><code>OUT.write(re.sub(r'(?&lt;=\d{2}:\d{2}:\d{2})\s*INFO\b', ' INFO2', f))
  </code></pre>
  
  <p>Now this does replace INFO with INFO2 but it also stops all the code below it from working. But it's dependent on where I place the code. If I place it after all of my other code, it doesn't seem to do anything, if I place it straight after where I define my IN and OUT then it breaks all formatting from the code below it</p>