0

I am struggling to apply a filter on a TexBox. I have 3 filter types:

1) Only Numbers (positive) Example: 123 good, -123, ad8, 12.0 not good

2) Only Numbers plus "^" and "." chars Example: 123^3, -34, -34.5 good, ad8, 23-4 not good

3) Only POSITIVE Numbers plus "^" and "." chars Example: 123^3, 34.5 good, -34, ad8, 23-4 not good

Here is my work:

private void PriceInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;

            switch (FieldType)
            {
                case InputFieldType.TypeQuantity:
                    tb.Text = KeyFilter.ExtractNumbersOnly(tb.Text);
                    break;
                case InputFieldType.TypePositivePrice:
                    tb.Text = KeyFilter.ExtractPositivePricesOnly(tb.Text);
                    break;
                case InputFieldType.TypePrice:
                    tb.Text = KeyFilter.ExtractPricesOnly(tb.Text);
                    break;
            }
        }

and KeyFilter:

public static string ExtractNumbersOnly(string s)
{
    Match match = System.Text.RegularExpressions.Regex.Match(s, "\\d+");

    return match.Value;
}

public static string ExtractPricesOnly(string s)
{
    Match match = System.Text.RegularExpressions.Regex.Match(s, "^[-]?\\d+([.]\\d+)?$");

    return match.Value;
}

public static string ExtractPositivePricesOnly(string s)
{
    Match match = System.Text.RegularExpressions.Regex.Match(s, "^[+]?\\d+([.]\\d+)?$");

    return match.Value;
}
1
  • 1
    You forgot to allow for the "^" character in 2/3. Should be: ^(?:[-]?\\d+(?:[.]\\d+)?)(?:\^[-]?\\d+(?:[.]\\d+)?)?$ and ^(?:\\d+(?:[.]\\d+)?)(?:\^[-]?\\d+(?:[.]\\d+)?)?$ respectively. This allows for things like -5.5^1.25 and 5.5^-1.25 respectively. Commented Mar 11, 2014 at 17:43

3 Answers 3

1

(1)

^\\d+$

(2)

^-?\\d+(\\.\\d+)?(\\^)?(-?\\d+(\\.\\d+)?)?$

(3) actually easier than (2). Just take (2) and remove the negative signs.

^\\d+(\\.\\d+)?(\\^\\d+(\\.\\d+)?)?$
Sign up to request clarification or add additional context in comments.

2 Comments

I tried ^-?\\d+(\\.\\d+)?(\\^-?(\\.\\d+)?)?$ but now now does not let me to enter 123^4, only 123^ or 123
@AlexandruCircus I have incorporated your suggestion for (2). Was designed to NOT take 123^
1

The required three regex are sequentially(as you asked) at below:

^\d+$

^-?\d+[.^]\d+$

^\d+[.^]\d+$

Please escape the \ with your escape character in C#

3 Comments

^-?\\d+[.^]\\d+$ does not find any match when I enter digit. eg:8
@AlexandruCircus It wasn't in your example. place a ? after [.^]. Which means [.^]?
@AlexandruCircus Sorry, it was. I missed it!
1
Regex.IsMatch("123", "^\\d+$")
Regex.IsMatch("-123^33", "^-?\\d+((\\^\\d+)|(\\.\\d+))?$")
Regex.IsMatch("123^33", "^\\d+((\\^\\d+)|(\\.\\d+))?$")

Note that 2nd and 3rd does NOT work if ^ and . both are included, e.g. 123.44^2 <- as per given RegEx, this is NOT a correct input

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.