title: How to get year component from a String in Swift?
body: <p>I'm trying to do pattern matching with the input string. I'm able to get other keywords which are required for processing. </p>
  
  <p>Currently I'm getting year component by comparing, if the input string has, "2019" or "1991", and so on. Sample code below:</p>
  
  <pre><code>let year = ["1975", "1976", "1977", ..... "2019"]
  
  func getYear(inputString: String) -&gt; String {
    for eachWord in inputString {
      if (year.contains(eachWord)) {
        return eachWord
      }
    }
    return ""
  }
  </code></pre>
  
  <p><strong>Input 1:</strong> "Get me the number goals Ronaldo scored in 2019." <strong>Output Year 1:</strong> "2019"</p>
  
  <p><strong>Input 2:</strong> "Who scored most runs in World Cup Cricket 1991 ." <strong>Output Year 2:</strong> "1991"</p>
  
  <p><strong>Input 3:</strong> "Who was Hockey world player of the year in 1975" <strong>Output Year 3:</strong> "1975"</p>
  
  <p><strong>Question:</strong> But how to get year component from string, in Swift without checking for hard-coded values in input string? Or what is the best possible way to get the year component from the Input String?</p>