0

I have a string

<ul><li>Option to add embroidered text personalization below design<br/>for only $1.00 per shirt and free setup</li><li>Men&#39;s Sizes: XS-6XL</li><li>Individually folded and bagged with size sticker for easy distribution</li><li>Ready to ship in 7 business days after art approval</li></ul>

Trying to match

<li>Men&#39;s Sizes: XS-6XL</li>

I am looking to take only the last <li></li> set that contains words

So for li that contains sizes I am looking to run something like:

(<li>).*?\b[sS]izes[ :]{1}.*?<\/li>

but that selects the first <li> instance instead of the closest.

EDIT: I can't use a html parser here like HTMLAgilityPack.

Ya Wang
  • 1,758
  • 1
  • 19
  • 41
  • 2
    Hang on.. I've got a great link somewhere round here that talks in some depth about using Regex to parse HTML... – Caius Jard Mar 19 '21 at 20:20
  • Ok but I am not looking for a htmlparser like HTMLAgilityPack here – Ya Wang Mar 19 '21 at 20:21
  • ..[found it](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) .. By the way, have you thought about setting [RTL search direction on your Regex](https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-options), if you have a pattern that matches the first but you want the last? – Caius Jard Mar 19 '21 at 20:27
  • You say "*I am looking to take only the last `
  • ` set that contains words*" and also that you're "*[trying] to match `
  • Men's Sizes: XS-6XL
  • `.*" Did you mean you want to match the last `li` element that contains the words `Men's sizes`? – David Thomas Mar 19 '21 at 20:28
  • Eh. This tells me to forget about it and use htmlagilitypack. – Ya Wang Mar 19 '21 at 20:29