I am removing all non-numeric and non-decimal values using:
Regex.Replace("A1B2C3.D4E5F6.G7H8I9", @"[^-?\d+\.]", "");
Output: 123.456.789
The issue is the repeating decimals. I need to remove all but the first decimal.
So the output becomes: 123.456789
I’ve found the pattern "\.(?=.*\.)" but it outputs 123456.789. Which is the last decimal instead of the first.
Can anyone combine these two patterns into one that will also remove repeating decimals after the first decimal? Ty
You can replace all but the first
.by replacing the following:which you can easily integrate into your regex:
This only matches a dot if there is another dot preceding. Note that this regex doesn’t work on most other regex engines out there because it uses a variable-width lookbehind.