Text1 I want to remove the span so it looks like this: Text1 Whic" /> Text1 I want to remove the span so it looks like this: Text1 Whic" /> Text1 I want to remove the span so it looks like this: Text1 Whic"/>

How to match only one string and not another

92 Views Asked by At

This is String 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):

<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?

1

There are 1 best solutions below

0
Mshnik On BEST ANSWER

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.