Anand Vyas
Anand Vyas

Reputation: 131

REGEX Help required with a optional values

I need to have a regex to parse the below string:

{ "<div class="highlighttitle2">UNSPSC 43211701</div>" }

The whole string is option. The output I need is

UNSPC: 43211701

Please help.

I have tried..

.*?((?(?=ul).*?(?(?=div)|.*?\bUNSPSC\b.*?(?'UNSPSC'[^<]*)</div>)|.*?(?(?=div).*?\bUNSPSC\b.*?(?'UNSPSC'[^<]*)</div>|))|).*?((?(?=ul).*?(?(?=div)|.*?\bUNSPSC\b.*?(?'UNSPSC'[^<]*)</div>)|.*?(?(?=div).*?\bUNSPSC\b.*?(?'UNSPSC'[^<]*)</div>|))|)

Upvotes: 0

Views: 61

Answers (2)

kayleeFrye_onDeck
kayleeFrye_onDeck

Reputation: 6958

This will give back as few matches as possible (probably what you're looking for)

(UNSPSC\s\d+?(?=<))

It won't care how many digits there are but will give you only one match instead of a match per digit.

Upvotes: 1

Jeff
Jeff

Reputation: 14279

If you can guarantee that the string is always going to start with UNSPC and it is followed by numbers with no whitespaces, then your regex could be

(UNSPC \d*)

And your result, UNSPC 43211701, will be in the first capture group.

Upvotes: 1

Related Questions