Powershell and Regex

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of John Beck John Beck 2 years, 3 months ago.

  • Author
    Posts
  • #18333
    Profile photo of John Beck
    John Beck
    Participant

    I have a text file called input.txt. The file's content is similar to below:

    zzzpbxwer123@domain.com
    zzzpbxyx8t@domain.com
    123@domain.com
    xyz@domain.com
    (555)-123-4567
    zz@domain1.net

    I need to just get the line in the format zzzpbx*@domain.com. Meaning, all the strings I am looking to get start with zzzpbx and end with @domain.com. In between it could be any number of letters or digits. Any ideas?

    Thanks

  • #18342
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Use Select-String. You probably don't even need a regex, a simple wildcard zzzpbx*@domain.com would probably work.

  • #18343
    Profile photo of John Beck
    John Beck
    Participant

    Don,

    Thanks. However, the application where I need to use this does not allow for wild cards.

  • #18344
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Oh, okay. Well, a regex would just be something like zzzpbk[\w]+@domain\.com

    \w meaning letters and numbers, but not spaces or punctuation. Adjust as needed. There's about about_regular_expressions help file in PowerShell with the basic syntax, and I use regextester.com a lot to play with regexes and get them tweaked. There's also regexlib.com which has a lot of pre-made ones.

  • #18345
    Profile photo of John Beck
    John Beck
    Participant

    that would work. I was using the below but it obviously was missing few things

    gc input.txt | Select-String "^zzz\@domain.com"

You must be logged in to reply to this topic.