locked
Very basic question RRS feed

  • Question

  • I'm starting to play with Regular Expression and have a very basic question:

    I tried the regular expression ^\d$ thinking it would only recognize expressions with numbers, and here is why

    1) the ^ means the expression has to begin with whatever follows it, right? in this case a number
    2) the $ means the expression has to end with whatever precede it, right? in this case a number.

    if my assumptions were correct, it would not recognize a1 or aaaa4, but it does. Where am I wrong?

    Here is the little program I'm using to check my expressions.


    static

     

    void Main(string[] args)

    {

     

    if (Regex.IsMatch(args[1], args[0]))

     

    Console.WriteLine("Input matches regular expression");

     

    else

     

    Console.WriteLine("Input does not match entry");

    }

    Thanks in advance.

    Wednesday, April 8, 2009 5:26 AM

Answers

  • Hi VictorHurdagaci

    Thanks for your help with this. You were definably on to something. When I called the app this way TestRegExpr ^\d$ a1, a1 was accepted.

    I modified the app, to print the regular expression before evaluating it, it came back to "\d$", so it was removing the ^ and that is why a1 was been accepted.

    So if I call the app TestRegExpr ^^\d$ a1, the expression keeps the ^ and a1 is not accepted.

    Thanks for the help.




    • Marked as answer by bmunoz Wednesday, April 8, 2009 7:43 PM
    Wednesday, April 8, 2009 7:43 PM

All replies

  • bmunoz, your expression matches strings composed of one digits. So it matches "0", "1", "2", "3", "4", "5", "6", "7", "8" and "9".

    In order to match a number you must specify that a the digit can appear many times "^\d+$"

    The expression should not match "a1". Can you tell us how you call the executable? (what arguments are you passing)
    Wednesday, April 8, 2009 2:11 PM
  • Hi VictorHurdagaci

    Thanks for your help with this. You were definably on to something. When I called the app this way TestRegExpr ^\d$ a1, a1 was accepted.

    I modified the app, to print the regular expression before evaluating it, it came back to "\d$", so it was removing the ^ and that is why a1 was been accepted.

    So if I call the app TestRegExpr ^^\d$ a1, the expression keeps the ^ and a1 is not accepted.

    Thanks for the help.




    • Marked as answer by bmunoz Wednesday, April 8, 2009 7:43 PM
    Wednesday, April 8, 2009 7:43 PM