Home > Blockchain >  Remove all label tags inside a string
Remove all label tags inside a string

Time:05-19

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  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  style="cursor: d
efault;">Telephone</td>
            <td style="cursor: default;">[<label id="{ce68e02e-e9fd-40ee-9375-ee1b05972e9b}">Phone</label>]</td>
        </tr>
        <tr>
            <td  style="cursor: default;">Email</td>
  <td style="cursor: default;">[<label id="{411b580e-f7e9-4dd2-a70d-947385360cd0}">Email</label>]</td>
        </tr>
        <tr>
            <td  style="cursor: default;">Message</td>
            <td style="cursor: default;">[
<label id="{13e2ff23-135c-4c6d-beb4-2960a533cb98}">Your Message</label>]</td>
        </tr>
        <tr>
            <td  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

CodePudding user response:

This regex could work, you can use the -replace operator instead of the call to Regex.Replace:

(Get-Content path\to\file -Raw) -replace '<label id="\{[\d\w-] }">([a-z ] )<\/label>', '$1'

See https://regex101.com/r/3gbJEp/1 for details.

  • Related