Remove all label tags inside a string

Viewed 55

I want to remove all label tags from a string.

This is the input string.

<p>
<title>Contact Us</title>
</p>
<table dropzone="copy">
    <tbody>
        <tr>
            <td class="label" style="cursor: default;">Full Name</td>
            <td style=
"cursor: default;">[<label id="{0a4a7240-9606-416a-bf7b-ef11a47cca8e}">First name</label>] [<label id="{94263497-683b-46f9-ba0f-69f4c2736598}">Last name</label>]</td>
        </tr>
        <tr>
            <td class="label" style="cursor: d
efault;">Telephone</td>
            <td style="cursor: default;">[<label id="{ce68e02e-e9fd-40ee-9375-ee1b05972e9b}">Phone</label>]</td>
        </tr>
        <tr>
            <td class="label" style="cursor: default;">Email</td>
  <td style="cursor: default;">[<label id="{411b580e-f7e9-4dd2-a70d-947385360cd0}">Email</label>]</td>
        </tr>
        <tr>
            <td class="label" style="cursor: default;">Message</td>
            <td style="cursor: default;">[
<label id="{13e2ff23-135c-4c6d-beb4-2960a533cb98}">Your Message</label>]</td>
        </tr>
        <tr>
            <td class="label" style="cursor: default;">Company</td>
            <td style="cursor: default;">[<label id="{c3f22c3a-8fc1
-48a4-8d6a-fe346024ca2b}">Company</label>]</td>
        </tr>
    </tbody>
</table>
<p> </p>
<p> </p>

The label tag needs to be removed but value inside the string should not be removed

<label id="{0a4a7240-9606-416a-bf7b-ef11a47cca8e}">First name</label> will become First name

<label id="{ce68e02e-e9fd-40ee-9375-ee1b05972e9b}">Phone</label> will become Phone

<label id="{411b580e-f7e9-4dd2-a70d-947385360cd0}">Email</label> will become Email

<label id="{13e2ff23-135c-4c6d-beb4-2960a533cb98}">Your Message</label> will become Your Message

<label id="{c3f22c3a-8fc1-48a4-8d6a-fe346024ca2b}">Company</label> will become Company

I tried the following regex [Regex]::Match( $text, '(?s)<label(.*)">' ).Groups.Value but its not working.

Any suggestions would be appreciated

Thanks in advance

2 Answers

It is generally a bad idea to attempt to parse HTML with regular expressions.
Instead use a dedicated HTML parser as the HtmlDocument class

Example

function ParseHtml($String) {
    $Unicode = [System.Text.Encoding]::Unicode.GetBytes($String)
    $Html = New-Object -Com 'HTMLFile'
    if ($Html.PSObject.Methods.Name -Contains 'IHTMLDocument2_Write') {
        $Html.IHTMLDocument2_Write($Unicode)
    } else {
        $Html.write($Unicode)
    }
    $Html.Close()
    $Html
}

$Html = ParseHtml ' # Your Html
    <p>
    ...
    <p> </p>
'

$Html.getElementsByTagName('label') |ForEach-Object { $Null = $_.removeNode() }
$Html.body.innerHtml

<P></P>
<TABLE dropzone="copy">
<TBODY>
<TR>
<TD class=label style="CURSOR: default">Full Name</TD>
<TD style="CURSOR: default">[First name] [Last name]</TD></TR>
<TR>
<TD class=label style="CURSOR: d&#10;efault">Telephone</TD>
<TD style="CURSOR: default">[Phone]</TD></TR>
<TR>
<TD class=label style="CURSOR: default">Email</TD>
<TD style="CURSOR: default">[Email]</TD></TR>
<TR>
<TD class=label style="CURSOR: default">Message</TD>
<TD style="CURSOR: default">[ Your Message]</TD></TR>
<TR>
<TD class=label style="CURSOR: default">Company</TD>
<TD style="CURSOR: default">[Company]</TD></TR></TBODY></TABLE>
<P></P>
<P></P>
Related