user3091209
user3091209

Reputation: 81

Simple C# regex match issue

I am having problems extracting values from a given string using RegEx match, the string which I am working with is below.

533 x 1981mm, 35mm Thick - Non Fire Door: £33.14

The RegEx I have is, which works fine if the string is as follows

533 x 1981mm, 35mm Thick: £33.14

^(?<first>\d+)\s*x\s*(?<second>\d+)mm,\s*(?<third>\d+)mm Thick: £(?<price>\d+\.\d+)$

My question is, how can I change the RegEx to ignore anything between the last 'mm' and the '£' sign?

What my code does it extract millimetre measurements, converts them into inches and returns a string to my method. The rest of the code is as follows.

var first = Int32.Parse(match.Groups["first"].Value);
var second = Int32.Parse(match.Groups["second"].Value);
var third = Int32.Parse(match.Groups["third"].Value);
var price = Decimal.Parse(match.Groups["price"].Value, CultureInfo.InvariantCulture);

Thank you gurus!

Upvotes: 2

Views: 137

Answers (2)

MarkO
MarkO

Reputation: 2233

Use [^£]+ to get 1 or more characters which are not a £.

^(?<first>\d+)\s*x\s*(?<second>\d+)mm,\s*(?<third>\d+)mm[^£]+£(?<price>\d+\.\d+)$

Upvotes: 1

Rawling
Rawling

Reputation: 50114

Replace mm Thick: £ with mm.*?£.

The .*? means "match any character (.) any number of times, including zero (*), as few times as possible (?)"

Upvotes: 3

Related Questions