title: Regular Expression to extract city state and country from html
body: <p>I am using Outwit hub to scrape a website for city, state, and country (USA and Canada Only).  With the program I can use regular expressions to define the <strong>markers Before and After</strong> the text I wish to grab.  I can also define a format for the desired text. </p>
  
  <p>Here is a sample of the html:</p>
  
  <pre><code>&lt;td width="8%" nowrap="nowrap"&gt;&lt;/td&gt;                        
  &lt;td width="22%" nowrap="nowrap"&gt;&lt;strong&gt;
  BILLINGS, MT
  USA&lt;/strong&gt;&lt;/td&gt;
  &lt;td width="10%" align="right" nowrap="nowrap"&gt;
  </code></pre>
  
  <p>I have set up my reg.ex. as follows:</p>
  
  <p><strong>CITY - Before</strong> <em>(not formated as regex)</em></p>
  
  <pre><code>&lt;td width="22%" nowrap="nowrap"&gt;&lt;strong&gt;
  </code></pre>
  
  <p><strong>CITY - After</strong> <em>(accounts for state, territory, and provences)</em></p>
  
  <pre><code>/(,\s|\bA[BLKSZRAEP]\b|\bBC\b\bC[AOT]\b|\bD[EC]\b|\bF[LM]\b|\bG[AU]\b|\bHI\b|\bI[ADLN]\b|\bK[SY]\b|\bLA\b|\bM[ABDEHINOPST]\b|\bN[BLTSUCDEHJMVY]\b|\bO[HKNR]\b|\bP[AERW]\b|\bQC\b|\bRI\b|\bS[CDK]\b|\bT[NX]\b|\bUT\b|\bV[AIT]\b|\bW[AIVY]\b|\bYT\b|\bUSA|\bCanada)/
  </code></pre>
  
  <p><strong>STATE - Before</strong></p>
  
  <pre><code>\&lt;td width="22%" nowrap="nowrap"\&gt;\&lt;strong\&gt;\s|,\s
  </code></pre>
  
  <p><strong>STATE - After</strong></p>
  
  <pre><code>/\bUSA\&lt;\/strong\&gt;\&lt;\/td\&gt;|\bCanada\&lt;\/strong\&gt;\&lt;\/td\&gt;/
  </code></pre>
  
  <p><strong>STATE - Format</strong></p>
  
  <pre><code>/\b[A-Z][A-Z]\b/
  </code></pre>
  
  <p><strong>COUNTRY - Before</strong> <em>(accounts for state, territory, and provences)</em></p>
  
  <pre><code>/(\bA[BLKSZRAEP]\b|\bBC\b\bC[AOT]\b|\bD[EC]\b|\bF[LM]\b|\bG[AU]\b|\bHI\b|\bI[ADLN]\b|\bK[SY]\b|\bLA\b|\bM[ABDEHINOPST]\b|\bN[BLTSUCDEHJMVY]\b|\bO[HKNR]\b|\bP[AERW]\b|\bQC\b|\bRI\b|\bS[CDK]\b|\bT[NX]\b|\bUT\b|\bV[AIT]\b|\bW[AIVY]\b|\bYT\b)\s/
  </code></pre>
  
  <p><strong>COUNTRY - After</strong> <em>(not formated as regex)</em></p>
  
  <pre><code>&lt;/strong&gt;&lt;/td&gt;&lt;td width="10%" align="right" nowrap="nowrap"&gt;
  </code></pre>
  
  <p>The issue arrises when there is no city or state listed.  I have tried to account for this, but am just making it worse.  Is there any way this can be cleaned up and still account for the possibility of missing info?  Thank you.</p>
  
  <p>Example with no city:</p>
  
  <pre><code>&lt;td width="8%" nowrap="nowrap"&gt;&lt;/td&gt;                        
  &lt;td width="22%" nowrap="nowrap"&gt;&lt;strong&gt;
  MT
  USA&lt;/strong&gt;&lt;/td&gt;
  &lt;td width="10%" align="right" nowrap="nowrap"&gt;
  </code></pre>
  
  <p>Example with no city / state: (yes, there is an extra line break)</p>
  
  <pre><code>&lt;td width="8%" nowrap="nowrap"&gt;&lt;/td&gt;                        
  &lt;td width="22%" nowrap="nowrap"&gt;&lt;strong&gt;
  
  USA&lt;/strong&gt;&lt;/td&gt;
  &lt;td width="10%" align="right" nowrap="nowrap"&gt;
  </code></pre>
  
  <p>Thank you for any help you can provide. </p>