DHARMINDER
DHARMINDER

Reputation: 141

How to put more than one or condition in regex

Below are the cases to cover using regular expression:

Case 1: (3) x 4.5 mL Red Top Tubes.

Case 2: (3) 4mL Red Top Tubes.

Case 3: (3)- 4mL Red Top Tubes.

I have created regular expression to cover all these above cases:

But it is not working for 'Case 3 ' where separator is '-'

(\(([^)]+)\)(?:\s*x\s*|\s*|-)(\d*\.\d+|\d+)\s*ml\s([\w\s]+)[,\;\and\s]*)

Output enter image description here

Thanks in advance !!

Upvotes: 0

Views: 41

Answers (1)

revo
revo

Reputation: 48711

Your regex doesn't match on - since second alternative \s* is an all-time match that satisfies engine right there. Your regex don't need all those alternations either:

\(([^)]+)\)\s*[x-]?\s*(\d+(?:\.\d+)?)\s*m[lL]\b([\w\s]+)

Live demo

Upvotes: 1

Related Questions