Hi how do you extract multiple decimals with different number of decimal places from a string?
I’m looking to find a generic way to extract 3 numbers out the following strings.
e.g
CC77X1722X12 => 77,1722,12
PC77.5X10102X12.5 => 77.5, 10102, 12.5
XP60.25X0.333X12 => 60.25, 0.333, 12
The three numbers are always separated by ‘X’, and the string always starts with 2 characters
Thanks!
Since you have such a specific pattern, you don’t even need to use regular expressions. Because the first two characters can be ignored and all the numbers are separated by ‘X’ characters, this C# code should do the trick (with appropriate error handling added, of course)
For production code, though, I would recommend decimal.TryParse over
decimal.Parse. To use that method, you could write something like