title: Parsing Dates from Text in R
body: <p>I repeatedly come across the problem to parse dates from relatively unstructured text documents where the date is embedded in the text and its position and format varies from case to case. Some example text is:</p>
  
  <pre><code>"Name of the city, name of the country, July 1st, 2015 - The group announces that it has completed the project initiated in November 2011. It has launched 12 other initiatives. The average revenue per initiative is USD 100."
  </code></pre>
  
  <p>I would like to extract the date string <code>"July 1st, 2015"</code> from the text (step 1) and convert it to a format like, for example, <code>2015-07-01 UTC</code> (step 2). Step 2 can be performed using, for example, <code>parse_date_time</code> from package <code>lubridate</code> (which is nice for multiple applicable date formats): </p>
  
  <p><strong>Case 1:</strong></p>
  
  <pre><code>library(lubridate)
  parse_date_time("July 1st, 2015", "b d Y", local="C")
  [1] "2015-07-01 UTC"
  </code></pre>
  
  <p>For some cases <code>parse_date_time</code> also works on larger strings which include the date. For example: </p>
  
  <p><strong>Case 2:</strong></p>
  
  <pre><code>parse_date_time("Name of the city, name of the country, July 1st, 2015 - The group announces that it has completed the project initiated in November", "b d Y" , local="C")
  [1] "2015-07-01 UTC"
  </code></pre>
  
  <p>However, as far as I understand it, step 2 does not work directly on the full example text:</p>
  
  <p><strong>Case 3:</strong></p>
  
  <pre><code>parse_date_time("Name of the city, name of the country, July 1st, 2015 - The group announces that it has completed the project initiated in November 2011. It has launched 12 other initiatives. The average revenue per initiative is USD 100.", "b d Y" , local="C")
  [1] NA
  </code></pre>
  
  <p>Apparently, some of the additional information in the text makes it cumbersome to parse the date directly from the full text. I can think of an approach where step 1 is performed using regex to extract a reduced string (similar to Case 1 or Case 2) that includes the date and for which <code>parse_date_time</code> works. However, using regex in connection with dates seems always a bit dirty as regex does not know whether it extracts a valid date. </p>
  
  <p>Is there a way to directly perform step 2 (i.e., without a workaround based on regex) on unstructured texts as in the above example (Case 3)?</p>
  
  <p>Any input is very much appreciated!</p>