28

I've been searching around for a little while to figure out how to confirm a string begins with a number. I came across a regex statement '/^\d/' which I can confirm says if starts with digit. However I can not seem to get it to work in the code below. Where did I went wrong in the statement if(Regex.Match(info,"/^\d/"))?

//String attachbody is the attachment body from an email C read into a string
string[] data = Regex.Split(attachbody, "\n");

foreach (String info in data)
{
    if (Regex.Match(info,"/^\d/"))
    {
        string[] tabbedHeaderData = Regex.Split(info, "\t");
        TicketID = tabbedHeaderData[0].ToString();
        Status = tabbedHeaderData[1].ToString();
        URL = tabbedHeaderData[2].ToString();
        InitCats = tabbedHeaderData[3].ToString();
        PostRevCats = tabbedHeaderData[4].ToString();
        ListNumClosed = tabbedHeaderData[5].ToString();

        Console.WriteLine(TicketID);
        Console.WriteLine(Status);
        Console.WriteLine(URL);
        Console.WriteLine(InitCats);
        Console.WriteLine(PostRevCats);
        Console.WriteLine(ListNumClosed);
        Console.ReadLine();
    }
}

example data (tab delimited):
TicketID Status URL InitCats PostRevCats ListNumClosed 555555 Closed http://5555555.com/searcho Malicious Sites 55555

Cœur
  • 37,241
  • 25
  • 195
  • 267
toosweetnitemare
  • 2,226
  • 8
  • 33
  • 44

6 Answers6

71

Your regex is wrong. /.../ is javascript convention for defining regular expressions. Try like this in C#:

if (Regex.IsMatch(info, @"^\d"))

Also notice that you should use the IsMatch method which returns boolean or your code won't even compile.

And if you wanted to match that the string starts with one or more digits:

if (Regex.IsMatch(info, @"^\d+"))
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    / ... / is not "javascript convention". It's perl. – erlando Jun 16 '11 at 13:07
  • 1
    @erlando, oh, pardon my ignorance then. I didn't knew this was coming from perl. Probably because I never used perl in my life :-) I just happen to use this convention in javascript. – Darin Dimitrov Jun 16 '11 at 13:08
  • 1
    Filed under "obscure knowledge" then.. :-) Javascript's regex engine uses perl-style regex's inline if not defined with new RegExp(...) in which case the regex should not be enclosed in /.../ – erlando Jun 16 '11 at 13:14
  • Also, if you specify the regex operation in Perl, you will be able to use odd characters as delimiter - i.e. m|^\d| instead of m/^\d/ is acceptable, albeit a bit unnecessary in this case. – SHODAN Jun 16 '11 at 13:50
42

You don't need a regex for this. Try

if (info.Length > 0 && char.IsDigit(info[0]))
{
   ...
}

If you want to use the regex, take out the // so it's just Regex.IsMatch(info,@"^\d").

agent-j
  • 27,335
  • 5
  • 52
  • 79
  • Thank you agent-j, this is exactly what i was looking for. Stackoverflow says i have to wait 20min to accept your answer. thanks! – toosweetnitemare Jun 16 '11 at 13:11
  • agent-j i just noticed that your answer is using the regex.match option and not the regex.ismatch option. I had to give the answer to Darin because he set me straight with it. Thank you for the ground work! – toosweetnitemare Jun 16 '11 at 13:21
  • 2
    This is the solution when you use portable library where Regex not have support. If at all have Regex support better go for that performance wise – Ramakrishna Aug 25 '16 at 06:37
4

It's the format of the string that you've supplied to Regex.Match.

The correct format would be:

Regex.Match(info,@"^\d")

The @ means that escape characters (like the backward slash) are treated as normal characters. Without it your regex would need to be "^\\d".

Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
1

Your regex is wrong. .Net doesn't use perl-style regexs. The correct expression is @"^\d".

And you should use Regex.IsMatch(...) if you're not going to use the MatchCollection from .Match for anything:

RegEx.IsMatch(info, @"^\d")
erlando
  • 6,736
  • 4
  • 24
  • 29
0

If you do not want to use Regex (some inteview question platforms do not allow regex ) and Char.IsDigit(info[0]),

You can use if(info[0] => 0 && info[9] <= 9) to check if char is numeric or not.

Depends on requirements if you do not want to consider '.' as Digit (for e.g. Integer values only) do not use Char.IsDigit(info[0]) since this function returns true for '.'

Nainesh Patel
  • 488
  • 2
  • 5
  • 19
0

If your only trying to find out if the string begins with a number. Like above, you canskip using regex. Just use Substring, remove the first character and test if it is a number.

Now if your trying to remove the entire number, then I would convert the string to a char array, and test the chars.

pseudo string s = "" for each c in chars{ if c is number then s += c else exit for } convert the s to an integer, you got the number at the begining of the string.

Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42