Powershell: Mulitple match in the same string using regex

19 Feb

I have a variable $a = “Kelvin (kel1), Mary (mar2), Tim (tim4)”, which I was to use match to extract all the text within the brackets.

If you use $a -match “\(\w*\)”, you will end up with only the first match (that is “(kel1)”, but I am wanted 3 matches within the brackets to be returned.

After digging into this site:, I found what was looking for. The below expression should extract multiple matches within a single string.

[regex]::Matches($a,’\(\w*\)’,’MultiLine’) | % {$_.Value}

Leave a comment

Posted by on February 19, 2013 in powershell, Scripts



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: