RSS

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: http://www.powershelladmin.com/wiki/Powershell_regular_expressions, I found what was looking for. The below expression should extract multiple matches within a single string.

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

Advertisements
 
Leave a comment

Posted by on February 19, 2013 in powershell, Scripts

 

Tags:

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: