powershell - regex email : extract domain with multiple dot -


i have email addresses john@domain.com.ch , john@domain.org. i'm using regex pattern :

$pattern="(?ms)@(.*?)\." 

and managed extract 'domain' john@domain.org not john@domain.com.ch. please me how 'domain' both of email addresses. thank you.

there flaws if there other @ signs in text (like twitter handle) keeping simple match after @ until first space. consider following bacon ipsum

bacon ipsum dolor amet ham hock shoulder pastrami ham andouille fatback john@domain.com.ch frankfurter ribeye pork. picanha pig frankfurter, ground round shank prosciutto doner flank. alcatra doner chicken pork chop shoulder, fatback turkey sausage flank picanha. john@domain.org

meatloaf short loin pancetta turkey.

rump sirloin meatball, shoulder @ ground round biltong beef ribs kielbasa spare ribs chicken capicola flank drumstick. jowl cow short loin pastrami biltong filet mignon rump pork chop capicola alcatra.

running regex @([^\s]+) results (from first capture group)

domain.com.ch domain.org 

if above text raw string in variable $text following line generate

$text | select-string "@([^\s]+)" -allmatches | select-object -expand matches | select-object -expand value 

output on console

@domain.com.ch @domain.org 

you use behinds omit @ easier remove in post process inefficient use of behinds here. or if don't mind code can use capture group exists here well

select-string "@([^\s]+)" -allmatches |        select-object -expandproperty matches |        select-object groups |        foreach{$_.groups[1]} |        select-object -expandproperty value 

each matches object returned has 2 groups. first entire capture , first our capture group.

if wanted more critical matches use @([a-z1-9\.-]+) i think allow accepted domain characters (minus couple of unicode characters allowed in tlds). if filter out matches without periods set.


Comments