title: How to detect a certain pattern in subsequent lines and appen/prepend text to it in python 3
body: <p>First of all I'd like to thank everyone who's going to give a hand. I'd need some help in writing code with python3. Briefly, I'm trying to write a script to clean up gcode files generated with Cura. The difficulty for me is to find a way to tell the program to append or prepend some text when it encounters a certain pattern made of 2 subsequent lines:</p>
  <pre><code>line 1 : G0 X(random number) Y(random number)
  line 2 : G1 X(random number) Y(random number)
  </code></pre>
  <p>Basically I want the program to recognize the &quot;pattern&quot; represented by the strings 'G0' and 'G1' located at the beginning of two different and subsequent lines. After that I want to append/prepend some text to this block-pattern.</p>
  <p>Example of some raw gcode:</p>
  <pre><code>G0 X85.84 Y107.16
  G1 X85.84 Y102.84 
  G1 X124.16 Y102.84 
  G1 X124.16 Y107.16 
  G1 X85.84 Y107.16 
  G0 X85.28 Y107.72
  G1 X85.28 Y102.28 
  G1 X124.72 Y102.28 
  G1 X124.72 Y107.72 
  G1 X85.28 Y107.72 
  G0 X85.28 Y107.52
  G0 X85.91 Y107.09
  G0 X86.95 Y106.05
  G1 X86.95 Y103.95 
  </code></pre>
  <p>Example of gcode with prepended and appended text in the right position:</p>
  <pre><code>hull_polyline3d (               
      points = [
          G0 X85.84 Y107.16
          G1 X85.84 Y102.84 
          G1 X124.16 Y102.84 
          G1 X124.16 Y107.16 
          G1 X85.84 Y107.16 
     ]
  );
  </code></pre>
  <p>#''' hull_polyline3d (</p>
  <h1>points = [ '''  --------&gt; is the text I want to prepend</h1>
  <p>#'''  ]</p>
  <h1>); ''' --------&gt; is the text I want to append</h1>
  <p>I hope the explanation was not too complicated. I've tried using Regex but I'm doing something wrong because the Editor returns an endless list of None as result of the enumeration. Here is the code I've written so far.</p>
  <p>//Code to make a preliminary clean up. Not really interesting for You since You don't have the gcode file. You could also try with a gcode file of your own.</p>
  <pre><code>import re
  
  inizio_estrusione = '''               #text to prepend
  
  hull_polyline3d (
  
      points = [
  '''
  
  fine_estrusione = '''                 #text to append
      ]
  );
  
  '''
  
  cleaned_lines = []                   #list where to store partially cleaned 
                                       #lines
  
  with open (&quot;C:\\Users\\leo\\Desktop\\Programma revisited\\dev.gcode&quot;, &quot;r&quot;) as fileHandler:
          
          letters = ['a', 'b', 'c', 'd',   #list of letters I want out of file
                     'h', 'i', 'l', 'm',
                     'n', 'o', 'p', 'q',
                     'r', 's', 't', 'u',
                     'v']
  
          patterns = ['F[0-9. ]+',         #list of patterns I want out of file
                  'E[0-9.\- ]+']
  
          for k in fileHandler:
  
              condition1 = (k.startswith('G1') | k.startswith('G0'))
  
              condition2 = (not any(letter in k for letter in letters))
  
              if condition1 &amp; condition2:
  
                  for p in patterns:                      #re function to 
                                                          #substitute the above 
                                                          #patterns with an 
                                                          #empty string
  
                      k = re.sub(p, '', k)
  
                  cleaned_lines.append(k)
  </code></pre>
  <p>#Here is where I tried to compile the pattern I've spoken of with the Regex but haven't succeeded.</p>
  <pre><code>extr_bgn_re = re.compile ('G0\n'+'G1')
  
  for index, q in enumerate(cleaned_lines):
  
      mo = extr_bgn_re.match(q)
  
      print(mo)
  </code></pre>