I'm using C# and .NET and I have a Regex that looks like this
"\D"
That matches all non-numeric characters however I don't want that to match a decimal point (.) and a negative sign (-). How can I do that with regular expressions?
So I tried Chris' and it made a few adjustments to make it work:
(I have a TextBox with a name of "Original")
private void Original_TextChanged(object sender, EventArgs e) {
Regex regex = new Regex(@"[^\d.-]", RegexOptions.IgnoreCase);
Match match = regex.Match(Original.Text);
if (match.Success) {
Original.Text = regex.Replace(Original.Text, "");
Original.SelectionStart = Original.TextLength;
}
}
This Original.SelectionStart = Original.TextLength;
is because whenever it was replaced it put the selection to the beginning and that would seem a little weird to a user...