Link to home
Start Free TrialLog in
Avatar of chad
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 + "asldjaflsdkjfalskjdflkajkcat@dog2'alsdkjf;alsdkjafoeidjfke

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
Avatar of soostibi
soostibi
Flag of Hungary image

What about this:
$a | Select-String "cat@(\w+)'" -AllMatches | %{$_.matches[0].groups[1].value}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of soostibi
soostibi
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If multiple dogs can be there in each line:
$a | Select-String "cat@(\w+)'" -AllMatches | %{$_.matches} | %{$_.groups[1].value}

Open in new window

Avatar of chad
chad

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chad

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