I need a Regular expression that allows me to split the following string in c#:
$1.89 BROWN RICE ‐ 16 03/01 ‐ 03/07 1.29
into something like this:
- Group 1: BROWN RICE – 16
- Group 2: 03/01 ‐ 03/07
- Group 3: 1.29
Is it possible to achieve this with Regex?
In your case I think a regex will be better than splitting.
If it’s original price (Product – Qty) (date range) (sale price), you can try something like
Title & Quantity are in captured group 1, date range in group 2, new price in group 3.
Explanation:
\$?\d+\.\d{2}: a price, optional dollar sign, exactly two decimal places (for the cents). If you want to allow ‘$1’ (ie no decimal places) then modify accordingly.([A-Za-z ]+- *\d+)Object name and quantity (separated by a hyphen). You may wish to modify this regex depending on the expected names you will get in (perhaps they aren’t just consisting of letters and spaces).(\d{2}/\d{2} *- *\d{2}/\d{2})date range. I have no idea if yours is month/day or day/month, but depending on that you can make your regex a lot more exclusive if you wish (for example, a numeric date is([012]\d|3[01])and a month only goes from 1 to 12).\$?(\d+\.\d{2})the saleprice.