0

试图找出一种使用正则表达式将所有多行 html td 组合转换为一行的方法,除了嵌套在另一个 td 中的那些

我正在尝试在visual studio中进行查找和替换(但我可以使用wildedit等其他东西)来查找跨多行运行的所有td标签,并将它们全部放在一行上。我想删除所有退货/标签。但问题是,如果我在其中嵌套了另一个表,我不想对父 td 执行此操作。

所以例如我想改变这个:

<table class="Top">
    <tr>
        <td class="TopLeft">
            <img src="img/spacer.gif" class="Size">
        </td>
        <td class="TopTile">
            <img src="img/spacer.gif" class="Size">
        </td>
        <td class="TopRight">
            <img src="img/spacer.gif" class="Size">
        </td>
    </tr>
    <tr>
        <td class="LeftTile">
            &nbsp;
        </td>
        <td class="TitleBar">
            Blah Blah Blah
        </td>
        <td class="RightTile">
            &nbsp;
        </td>
    </tr>
    <tr>
        <td class="LeftTile">
            &nbsp;
        </td>
        <td>
            <table cellpadding="2" cellspacing="0" border="0" class="EntryLight">
                <tr>
                    <td class="TopLeft">
                        <img src="img/spacer.gif" class="Size">
                    </td>
                    <td class="TopTile">
                        <img src="img/spacer.gif" class="Size">
                    </td>
                    <td class="TopRight">
                        <img src="img/spacer.gif" class="Size">
                    </td>
                </tr>
                <tr>
                    <td class="LeftTile">
                        &nbsp;
                    </td>
                    <td class="TitleBar">
                        Blah Blah Blah
                    </td>
                    <td class="RightTile">
                        &nbsp;
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

进入这个:

<table class="Top">
    <tr>
        <td class="TopLeft"><img src="img/spacer.gif" class="Size"></td>
        <td class="TopTile"><img src="img/spacer.gif" class="Size"></td>
        <td class="TopRight"><img src="img/spacer.gif" class="Size"></td>
    </tr>
    <tr>
        <td class="LeftTile">&nbsp;</td>
        <td class="TitleBar">Blah Blah Blah</td>
        <td class="RightTile">&nbsp;</td>
    </tr>
    <tr>
        <td class="LeftTile">&nbsp;</td>
        <td>
            <table cellpadding="2" cellspacing="0" border="0" class="EntryLight">
                <tr>
                    <td class="TopLeft"><img src="img/spacer.gif" class="Size"></td>
                    <td class="TopTile"><img src="img/spacer.gif" class="Size"></td>
                    <td class="TopRight"><img src="img/spacer.gif" class="Size"></td>
                </tr>
                <tr>
                    <td class="LeftTile">&nbsp;</td>
                    <td class="TitleBar">Blah Blah Blah</td>
                    <td class="RightTile">&nbsp;</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
4

1 回答 1

1

如果您安装了 Visual Studio 2012,这适用于您的示例。这是使用 .NET 正则表达式库的第一个版本:

搜索

(?<=<td[^>]*>)(?>\s+)(?!<table)|(?<!</table>\s*)\s+(?=</td>)

并一无所有。

解释:

(?<=        # Assert that it's possible to match...
 <td[^>]*>  # an opening <td> tag
)           # before the current position,
(?>\s+)     # then match one or more whitespace characters possessively,
(?!<table)  # but only if the next tag isn't an opening <table> tag.
|           # Or:
(?<!        # (unless we're right after...
 </table>   #  a closing </table> tag
 \s*        #  which may be followed by whitespace)
)           # then
\s+         # Match whitespace
(?=</td>)   # until the next closing </td> tag
于 2012-10-10T21:17:37.817 回答