title: R: use grep to find one or several matches in order of importance
body: <p>I am using grep to tidy up some address data, my goal here specifically is to identify the street / avenue / road name etc. in a given record and column, which has already been split by space into individual words in the following variable <em>tempval</em>, for example:</p>
  
  <pre><code>R &gt; tempval
  [1] "38"   "WILLOW" "PARK"  
  </code></pre>
  
  <p>I use the following statement to spot where some of the word that will follow the street name might be:</p>
  
  <pre><code>  stID &lt;- grep("STREET|\\bST\\b|AVENUE|\\bAVE\\b|\\bAV\\b|WAY|BOULEVARD|\\bBD\\b|ROAD|\\bRD\\b|PLACE|\\bPL\\b|ESPLANADE|TERRACE|PARADE|DRIVE|\\bDR\\b|\\bPARK\\b|LANE|CRESCENT|\\bCOURT\\b|b\\CRES\\b", tempval, ignore.case = T)
  
  R &gt; stID
  [1] 3
  </code></pre>
  
  <p>This is fine, I know "PARK" is the 3rd element and what comes before that will be my street number and name. </p>
  
  <p>However a problem arises when there are several matches so <code>length(stID) &gt; 1</code>, for example:</p>
  
  <pre><code>R &gt; tempval
  [1] "38"   "PARK" "ST" 
  </code></pre>
  
  <p>So here, I get</p>
  
  <pre><code>R &gt; stID
  [1] 2 3
  </code></pre>
  
  <p>How do I get R to return only one match, in order of importance (the order in which I have placed the strings in the pattern of grep)? In other words, if R finds both "ST" and "PARK", "ST" is more important than "PARK" thus return <code>stID = 3</code> only?</p>