title: Regex to match URL in Powershell
body: <p>I am new to programming and <code>Powershell</code>, I've put together the following script; it parses through all the emails in a specified folder and extract the URLs from them. The script uses a regex pattern to identify the URLs and then extracts them to a text file. The extracted text is then run through another command where I am trying to remove the <code>http://</code> or <code>https://</code> portion (I need help with figuring this out), these are placed into another text file, from which I go through again to remove duplicates.   </p>
  
  <p>The main issue I am having is that the regex doesnt appear to extract the urls correctly. What I am getting is something like an example I have created below:  </p>
  
  <p>URL is <code>http://www.dropbox.com/3jksffpwe/asdj.exe</code> 
  But I end up getting  </p>
  
  <pre><code>dropbox.com/3jksffpwe/asdj.exe
  dropbox.com 
  drop  
  dropbox
  </code></pre>
  
  <p>The script is</p>
  
  <pre><code>#Adjust paths to location of saved Emails
  $in_files = ‘C:\temp\*.eml, *.msg’  
  $out_file = ‘C:\temp\Output.txt’  
  $Working_file = ‘C:\temp\working.txt'  
  $Parsed_file = ‘C:\temp\cleaned.txt'  
  
  # Removes the old output file from earlier runs.
  if (Test-Path $Parsed_file) {
    Remove-Item $Parsed_file
  }
  
  # regex to parse thru each email and extract the URLs to a text file
  $regex = ‘([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)*?’  
  select-string -Path $in_files -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } &gt; $out_file
  
  #Parses thru the output of urls to strip out the http or https portion  
  Get-Content $Out_file | ForEach-Object {$_.SubString(7)} | Out-File    $Working_file
  
  
  #Parses thru again to remove exact duplicates  
   $set = @{}  
   Get-Content $Working_file | %{  
     if (!$set.Contains($_)) {  
         $set.Add($_, $null)  
          $_  
      }  
  } | Set-Content $Parsed_file  
  
  
  #Removes the files no longer required  
  Del $out_file, $Working_file  
  
  #Confirms if the email messages should be removed  
  $Response = Read-Host "Do you want to remove the old messages? (Y|N)"  
  
  If ($Response -eq "Y") {del *.eml, *msg}  
  
  #Opens the output file in notepad  
  Notepad $Parsed_file  
  
  Exit   
  </code></pre>
  
  <p>Thanks for any help</p>