Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# string starts with a number regex

Tags:

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

like image 969
toosweetnitemare Avatar asked Jun 16 '11 13:06

toosweetnitemare


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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+")) 
like image 118
Darin Dimitrov Avatar answered Sep 28 '22 10:09

Darin Dimitrov