title: Using Python Regex to Automate Text Extraction
body: <p>I am working on an automation project that involves using a python script that parses a txt file and repeatedly extracts 3 lines of data that come after a keyword. To give context, the txt files contains hundreds of form letters that are the same except for the name and address at their end. My end goal is to step through all of these form letters and extract the names and address and put them into an Excel spreadsheet using openpyxl. Here is an example of what the txt document looks like...</p>
  
  <pre><code>Dear Sir,
  
  Body of letter that is the same for all of the letters. Etc.
  
  Sincerely,
  
  Mr. John Smith
  123 Any Street
  Anytown, USA 11111
  </code></pre>
  
  <hr>
  
  <pre><code>Dear Sir,
  
  Body of letter that is the same for all of the letters. Etc.
  
  Sincerely,
  
  [Ms.] [Jane] [Doe]
  [456 Some Lane]
  [Sometown, USA 22222]
  </code></pre>
  
  <hr>
  
  <p>etc. (These letters go on and on with different names that need to be extracted)</p>
  
  <hr>
  
  <p>My question is how can I use regex in python to set a "last word keyword" (in this particular batch of letters "Sincerely," but could change to something else like "Regards" in another separate txt file) and commit to memory each bracketed item shown above (brackets correspond to eventual Excel columns like suffix, first name, Last Name, address, town) and step through each of the hundreds of letters? Is this simple to do using regex or python in general? I think I can manage to do it for one letter, but am stuck at how to move on to the next end keyword for the next letter. If anyone has any thoughts on what would be the most efficient way to approach this, I would greatly appreciate it! Also, I know it is costumery to post my initial approach to the problem, however being completely unfamiliar to regex operations, my attempt was not even close. Also, let me know if I am doing something unorthodox with a Stack Overflow question as I am new here.</p>
  
  <hr>
  
  <p>Example input: letters input.txt <br/>
  email received 3/30/17:</p>
  
  <pre><code>Dear Sir,
  
  I would be delighted to be placed on your mailing list. Please keep up the great work!
  
  Sincerely,
  
  Mr. Thomas Dahlmer
  46 Alpine Street
  Evanston, Il 60201
  </code></pre>
  
  <p>email received 3/30/17:</p>
  
  <p>Dear Sir,</p>
  
  <p>I would be delighted to be placed on your mailing list. Please keep up the great work!</p>
  
  <p>Sincerely,</p>
  
  <p>Mr. Robert Nolan <br/>
  1104 Madison Avenue <br/>
  New York, NY 10021</p>
  
  <p>email received 3/30/17:</p>
  
  <pre><code>Dear Sir,
  
  I would be delighted to be placed on your mailing list. Please keep up the great work!
  
  Sincerely,
  
  Ms. Angela Carraway &lt;br/&gt;
  402 Arlington Drive &lt;br/&gt;
  Concord, MA 01742
  </code></pre>
  
  <hr>
  
  <p>example output <br/>
  lettersoutput.txt</p>
  
  <pre><code>Mr. Thomas Dahlmer &lt;br/&gt;
  46 Alpine Street &lt;br/&gt;
  Evanston, Il 60201
  
  Mr. Robert Nolan &lt;br/&gt;
  1104 Madison Avenue &lt;br/&gt;
  New York, NY 10021
  
  Ms. Angela Carraway &lt;br/&gt;
  402 Arlington Drive &lt;br/&gt;
  Concord, MA 01742
  </code></pre>