Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Regex match string if not preceded anywhere earlier by another string

I have a set of HTML tags and I'm attempting to capture a value out of them:

<div class="foo">lorem</div>
<div class="bar">12.34</div>
<div class="baz">ipsum</div>
<div class="foo">dolor</div>
<div class="bar">19.97</div>
<div class="qux">sit</div>

I'm attempting to match and capture the "12.34" out of the first instance of the <div> with class "bar". However, this tag may or may not exist. If it doesn't exist, I don't want to capture anything after the <div> with class "baz". The amount of HTML elements between the ones shown may also vary, so I need to account for that as well.

I'm still quite the rookie with Regex, but I've tried several variations of something like this to no avail (thus far):

(?<!class="baz">)\s*?<div class="bar">(\d*\.?\d+)<

Whether a negative lookbehind is the right approach, I'm unsure, but I'm open to any suggestions.

I'm attempting to do this through a proprietary .NET tool that someone else wrote, so I'm not clear what Regex engine is running but I want to say it's JavaScript?

Any ideas or leads? Thank you!

Comments