Regex to find any text separated by an @

Welcome Forums General PowerShell Q&A Regex to find any text separated by an @

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #67599

    Participant
    Points: 36
    Rank: Member

    Hi,

    I'm fighting to make a regex to find any text after a space and before an @ and any text before that until it reaches a whitespace again...

    $myString = @"
    dys dkan jdna user@domain.no hhaksda d asda user@domain.com
    dsad ada sdd asd manolo.andrade@dominio.com.br fdfsd 
    dsajklg test.user@domain.net
    "@

    Using this:

    (Select-String -InputObject $myString -Pattern '\w+@\w+\.\w+.\w+' -AllMatches).Matches | ft * -AutoSize

    I only get the andrade@dominio.com.br without the manolo...
    I want to find any text that has a @ in the middle... (Ex.: te@aol.com, test.teste_teste@test.com.br, etc)

  • #67602

    Participant
    Points: 144
    Helping Hand
    Rank: Participant

    You could use a pattern like that

    '\S+@\w+\.\w*\.*\w+'

    and it fits with the examples you posted but a little more sofisticated would be this I think

    '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b'

    😉

    • #67624

      Participant
      Points: 36
      Rank: Member

      Olaf Soyk,

      You're the man! Thanks a lot!

  • #67639

    Participant
    Points: 144
    Helping Hand
    Rank: Participant

    De nada. 😉

The topic ‘Regex to find any text separated by an @’ is closed to new replies.