Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match substrings not containing a specific substring

Tags:

regex

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

THE STRING

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

THE DESIRED OUTCOME:
The following line should be matched:

<a class="link-tbl">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 class="link.*?(?!href).*?<\/a>
like image 748
MK01111000 Avatar asked Sep 15 '26 13:09

MK01111000


1 Answers

You may use this regex:

<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>
like image 176
anubhava Avatar answered Sep 17 '26 13:09

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!