Using Regex in Powershell to Capture Email

I wrote a script to capture various fields in an HTML file and populate the variables with the results. I have problems with regex for email capture. Here is a sample code:

$txt='<p class=FillText><a name="InternetMail_P3"></a> First.Last@company-name.com </p>' $re='.*?'+'([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})' if ($txt -match $re) { $email1=$matches[1] write-host "$email1" } 

I get the following error:

 Bad argument to operator '-match': parsing ".*?([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\ .)+[a-zA-Z]{2,7})([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})" - [xy] range in reverse order.. At line:7 char:16 + if ($txt -match <<<< $re) + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : BadOperatorArgument 

What am I missing here? Also, is there a better regular expression for email?

Thanks in advance.

+4
source share
2 answers

In fact, any regular expression suitable for .Net or C # will work for PowerShell . And you can find tons and tons of samples in stackoverflow and inet. For example: How to find or verify an email address: Official Standard: RFC 2822

 $txt='<p class=FillText><a name="InternetMail_P3"></a> First.Last@company-name.com </p>' $re="[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" [regex]::MAtch($txt, $re, "IgnoreCase ") 

But there is another part of this answer. Regex is not inherently suitable for parsing XML / HTML . You can find more information here: Using regular expressions to parse HTML: why not?

To provide a real solution, I first recommend

  • convert html โ†’ xhtml
  • XML tree navigation
  • work with individual nodes one after another, even using a regular expression.
+7
source

When it comes to email authentication, I usually choose the short version of RFC 2822:

? [A-z0-9 # $% & !? * + / = ^ _ {|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_ {|} ~ -] +) * @ (?: a-z0-9) + a- z0-9?

Learn more about email verification here.

+2
source

All Articles