1

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.

2 Answers 2

3
$array = $lines -replace '<td>(.*?)</td>','$1'
Sign up to request clarification or add additional context in comments.

Comments

1
$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')}

6 Comments

hmm, this definitely replaces contents which is great, but it doesn't place it into an array. I believe it places the contents into a very long string. I need the array to do a .getCount and then run through another loop to build a csv. I have tried doing $array = @() and running the code snippet, but can't seem to figure it out. Any suggestions? Thanks!
Assuming that $lines is an array, $array is one as well. To test, I populated $lines with: $lines = Get-Content test.html
$lines is currently one long string and I break each <td> line into one row within the array using the [regex] command
There is probably a better way to do this, but I came up with this, combining your first RegEx query, with the '$1' trick suggested by mjolinor: $array = [regex]::matches($lines, "<td>(.*?)</td>") | Select -exp Value | % {$_ -replace "<td>(.*?)</td>", '$1'}
I just updated my answer with what I think is a much better solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.