如何只匹配一个字符串而不是另一个字符串

时间:2021-05-10 19:23:55

This is String 1:

这是字符串1:

<td class="AAA"><span class="BBB">Text1</span></td>

I want to remove the span so it looks like this:

我想删除跨度,所以它看起来像这样:

<td class="BBB">Text1</td>

Which is easy enough with this regex:

这个正则表达式很容易:

Search: <td class="AAA"><span class="BBB">(.*)</span></td>
Replace: <td class="BBB">$1</td>

The problem: Sometimes the string looks like this (String 2):

问题:有时字符串看起来像这样(字符串2):

<td class="AAA"><span class="BBB">Text1</span>-<span class="BBB">Text2</span></td>

which also matches because of the 2 closing tags. But I don't want it to be matched at all. How do I find only String 1?

因为2个结束标记也匹配。但我不希望它完全匹配。我如何只找到字符串1?

1 个解决方案

#1


1  

Instead of matching any character in your matching group, match all characters aside from the open <:

不是匹配匹配组中的任何字符,而是匹配除开放<:之外的所有字符:

Search: <td class="AAA"><span class="BBB">([^<]*)</span></td>
Replace: <td class="BBB">$1</td>

This is assuming your Text1 doesn't contain the < character.

这假设您的Text1不包含 <字符。< p>

#1


1  

Instead of matching any character in your matching group, match all characters aside from the open <:

不是匹配匹配组中的任何字符,而是匹配除开放<:之外的所有字符:

Search: <td class="AAA"><span class="BBB">([^<]*)</span></td>
Replace: <td class="BBB">$1</td>

This is assuming your Text1 doesn't contain the < character.

这假设您的Text1不包含 <字符。< p>