Wednesday, 15 February 2012

powershell - How van I get 2 matches in a regex? -



powershell - How van I get 2 matches in a regex? -

i have xml files formatted this:

<user> <firstname>foo bar</firstname> <companyname>foo</companyname> <emailaddress>bar@foo.com</emailaddress> </user> <user> ...

i want read through xml files, creating output <companyname>,<emailaddress>, so:

foo,bar@foo.com user2,user@email.com blah,blah@blah.com

i using next snippet:

$directory = "\\pc001\blah" function getfiles ($path) { foreach ($item in get-childitem $path) { if ( test-path $item.fullname -pathtype container) { getfiles ($item.fullname) } else { $item } } } foreach ($file in getfiles($directory)) { if ($file.extension -eq '.test') { $content = get-content $file.fullname $pattern = '(?si)<companyname>(.*?)</companyname>\n<emailaddress>(.*?)</emailaddress>' $matches = [regex]::matches($content, $pattern) foreach ($match in $matches) { $matches[0].value -replace "<.*?>" } } }

however, $matches empty there's wrong regex. if leave out \n<emailaddress>(.*?)</emailaddress>, works. doing wrong?

$pattern = '(?si)<companyname>(.*?)</companyname>\s*<emailaddress>(.*?)</emailaddress>'

try this.\s create sure spaces , newlines covered.

regex powershell

No comments:

Post a Comment