title: Diacritics and regular expressions in R
body: <p>In R I have a column which should contain only one word. It is created by taking the contents of another column and with regex only keeping the last word. However, for some rows this doesn't work in which case R simply copies the content from the first column. Here is my R</p>
  
  <pre><code>df$precedingWord &lt;- gsub(".*?\\W*(\\w+-?)\\W*$","\\1", df$leftContext, perl=TRUE)
  </code></pre>
  
  <p>precedingWord should only hold one word. It is extracted from leftContext with regex. This works fine overall, <strong>but</strong> not with diacritics. A couple of rows in leftContext have letters with diacritics such as <code>é</code> and <code>à</code>. For some reason R <em>ignores</em> these items completely and simply copies the whole thing to precedingWord. I find this odd, because it is practically impossible that the regex matches the whole thing - as you can see <a href="http://df$precedingWord%20%3C-%20gsub(%22.*?%5C%5CW*(%5C%5Cw%20-?)%5C%5CW*$%22,%22%5C%5C1%22,%20df$leftContext,%20perl=TRUE)" rel="nofollow">here</a>. In the example, <em>Test string</em> is leftContext and <em>Substitution</em> should be *precedingWord.</p>
  
  <p>As you see in the example above, the output in the online regex tester is different from the output I get. I simply get an exact copy of leftContext. This does not mean that the output in the online tester is what want. Now the tool considers letters with diacritics as non-word characters and thus it doesn't mark it as the output that I want. But actually, I want to threat them as word characters so they are eligible for output.</p>
  
  <p>If this is the input:</p>
  
  <pre><code>Un premier projet prévoit que l'établissement verserait 11 FF par an et par élève du secondaire et 30 FF par étudiant universitaire, une somme à évaluer et à  
  Outre le prêt-à- 
  And à 
  Sur base de ces données, on cherchera à 
  Ce sera encore le cas ce vendredi 19 juillet dans l'é
  </code></pre>
  
  <p>Then this is the output I expect</p>
  
  <pre><code>à
  prêt-à-
  à
  à
  é
  </code></pre>
  
  <p>This is the regex I already have</p>
  
  <pre><code>.*?\W*(\w+?-?)\W*$
  </code></pre>
  
  <p>I'm already using stringi in my project, so if that provides a solution I could use that.</p>