I’m trying to write a regex to match patterns like this:
<td style="alskdjf" />
i.e. a self terminating <td>
but not this:
<td style=alsdkjf"><br /></td>
I initially came up with:
<td\s+.*?/>
but that obviously fails on the second example and I thought that something like this might work:
<td\s+.*?[^>]/>
but it doesn’t. I’m using C#.NET.
Only looking for <td>‘s that have an attribute. e.g. looking for <td style="alsdfkj" /> but not <td>.
This will match what you’re looking for, and not match the problematic case you had with your first few tries:
Note, however, that if you need to allow
>characters in attribute values, you’d need something like this:Which allows
>only within matching double-quotes (you could similarly expand it to allow single-quotes).You can add whatever specific attribute you’re looking for into the regex; for instance for your example: