Home > other >  How do I match substrings not containing a specific substring
How do I match substrings not containing a specific substring

Time:10-20

I'm trying to match a substring which does not contain a specific substring "href"

THE STRING

blabla bladibla <a  href="add-tt-2021-s18-chapter-4.1.html#ch-9-1-1-21-3">9.1.1.21.3</a> blabla <a  href="add-tt-2021-s18-chapter-4.1.html#ch-9-1-1-21-5">9.1.1.21.5</a> more blabla <a >tabel 9.1.1.21.6</a>, some more bladibla

THE DESIRED OUTCOME:
The following line should be matched:

<a >tabel 9.1.1.21.6</a>

WHAT I HAVE TRIED:
I tried a negative lookahead, but this still matches a-tags having href as a substring

<a lang-bash prettyprint-override"><a (?![^<>]*href)[^>]*>.*?<\/a>

RegEx Demo

RegEx Details:

  • <a : Match <a
  • (?![^<>]*href): Negative lookahead that makes sure that there is no href ahead after 0 or more of any char that are not < and >
  • [^>]*: Match 0 or more of any char that are not >
  • >: Match >
  • .*?: Match 0 or more of any characters
  • <\/a>: Match </a>
  • Related