chad
asked on
Powershell - Parse strings to build array
I have a string that contains about 200 characters and I would like to parse it for certain items.
[array]$a = "alsdkf laskdjlfkajsldjf asldkjf laksjdflk cat@dog1' alkjflkasdf asdfasdfasdf"
$a = $a + "asldjaflsdkjfalskjdflkajk cat@dog2'a lsdkjf;als dkjafoeidj fke
essentially I would like to have a result of from the above array.
dog1
dog2
the above string is read from a txt file and may contain single and double quotes.
the values I need will always follow a cat@ and will always be followed by a single quote.
I have tried working with trimstart and replace but I am missing an understanding of some of their features.
Thanks,
K
[array]$a = "alsdkf laskdjlfkajsldjf asldkjf laksjdflk cat@dog1' alkjflkasdf asdfasdfasdf"
$a = $a + "asldjaflsdkjfalskjdflkajk
essentially I would like to have a result of from the above array.
dog1
dog2
the above string is read from a txt file and may contain single and double quotes.
the values I need will always follow a cat@ and will always be followed by a single quote.
I have tried working with trimstart and replace but I am missing an understanding of some of their features.
Thanks,
K
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If multiple dogs can be there in each line:
$a | Select-String "cat@(\w+)'" -AllMatches | %{$_.matches} | %{$_.groups[1].value}
ASKER
Thanks Soostibi,
I will double the points if you could walk me through the code so I can better understand what I now have.
K
I will double the points if you could walk me through the code so I can better understand what I now have.
K
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again.
I really do not understand the piping yet. And anytime I see $_. I am lost. I need to work on understanding that.
Thanks,
K
I really do not understand the piping yet. And anytime I see $_. I am lost. I need to work on understanding that.
Thanks,
K
Open in new window