1
votes

I'm trying to get a regex working that will replace everything except for numbers and a decimal point (easy). The tricky part: the decimal point is optional but, if present, must be trailed by a further number.

So:

.10 => 10
10. => 10
10.- => 10
1.0 => 1.0

I'm not quite sure how to define the "except numbers followed by an optional decimal point but mandatory number after the optional decimal point" bit :)

Thanks!

1
Regexes are fun! But I would strongly recommend spending an hour or two studying the basics. For starters, you need to learn which characters are special: "metacharacters" which need to be escaped (i.e. with a backslash placed in front - and the rules are different inside and outside character classes.) There is an excellent online tutorial at: www.regular-expressions.info. The time you spend there will pay for itself many times over. Happy regexing!ridgerunner
Thanks for your comment. Regex is actually not new to me at all, I'm usually quite comfortable with regex but I've gotten rusty and I always have trouble with negated patterns for replace statements. CheersChristof

1 Answers

2
votes

It would be something like this:

\d+(\.\d+)?

(Please note that the regex syntax you are using may require different escaping.)