Reputation: 124
I have been trying to replace values within an array and I am currently trying to sanitize some of the values. I am trying to remove the table tags through the use of an array, or perhaps there is a better way to do the regex? Any help would be appreciated! Thanks.
$array = [regex]::matches($lines, "<td>(.*?)</td>")
for($x = 0; $x -le $max; $x++){
$array[$x] = $array[$x].value -replace "<td>", ""
}
Unfortunately, I continuously get the error below:
[ : Unable to index into an object of type System.Text.RegularExpressions.MatchCollection.
Upvotes: 1
Views: 3386
Reputation: 3163
$array = $lines | ? {$_ -match "<td>(.*?)</td>"} | % {$_.Replace("<td>", "").Replace("</td>", "") }
If you only want to replace , and not , just omit the second Replace.
Better yet, if you want to replace both:
$array = $lines | ? {$_ -match "<td>(.*?)</td>"} | % {$_ -replace "</?td>", ""}
In answer to your comments, this might work better:
$array = [regex]::matches($lines, "<td>(.*?)</td>") | Select -exp Value | % {$_ -replace "<td>(.*?)</td>", '$1'}
I suspect there is a better way to do this, as applying the regular expression twice seems inefficient, but I think it should work.
Okay, I think I've got it. Try this:
$array = [regex]::matches($lines, "<td>(.*?)</td>") | % {$_.Result('$1')}
Upvotes: 1