title: Regular expression to extract href url
body: <p>I want to extract the links from a String with regular expressions. I found a similar post <a href="https://stackoverflow.com/questions/21763865/regular-expression-in-ios-to-extract-href-url-and-discard-rest-of-anchor-tag">here</a> and I tried this code </p>
  
  <pre><code>let regex = try! NSRegularExpression(pattern: "&lt;a[^&gt;]+href=\"(.*?)\"[^&gt;]*&gt;.*?&lt;/a&gt;")
          let range = NSMakeRange(0, text.characters.count)
          let htmlLessString :String = regex.stringByReplacingMatches(in: text,
                                                                              options: [],
                                                                              range:range ,
                                                                              withTemplate: "")
  </code></pre>
  
  <p>but the proposed regular expression deleted all the content of the <code>href</code> tag. My string look like </p>
  
  <pre><code>SOME stirng  &lt;a href="https://com.mywebsite.com/yfgvh/f23/fsd" rel="DFGHJ"&gt; some text I need to keep &lt;/a&gt; and other text
  </code></pre>
  
  <p>and the expected result is</p>
  
  <pre><code>SOME stirng  https://com.mywebsite.com/yfgvh/f23/fsd some text I need to keep and other text
  </code></pre>
  
  <p>the perfect result is</p>
  
  <pre><code>SOME stirng some text I need to keep (https://com.mywebsite.com/yfgvh/f23/fsd) and other text
  </code></pre>
  
  <p>Do you have an idea if it's possible to achieve this?</p>