Regular expressions in C#
-
Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:
strFile = "C:\\test.txt";
using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
while ((strLine = srTlog.ReadLine()) != null)
{
Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
if(pattern.IsMatch(strToCheck))
{
//DO WHATEVER NOW
}
}
}I would appreciate any help. Thanks
-
Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:
strFile = "C:\\test.txt";
using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
while ((strLine = srTlog.ReadLine()) != null)
{
Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
if(pattern.IsMatch(strToCheck))
{
//DO WHATEVER NOW
}
}
}I would appreciate any help. Thanks
string patternStr = @"^\d{2}-\d{2}-\d{4} \d{2}:\d{2} \d{3}-\d{2}-\d{6}$"; ^ is beginning of line $ is end of line \d is any digit It should match the given string, although it will also match strings with invalid dates such as 99-99-2009 34:10 123-45-123456 But if you know that the datestring will always be correct and the important thing is the last bit, the pattern above should work fine (didnt test it though).
-
Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:
strFile = "C:\\test.txt";
using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
while ((strLine = srTlog.ReadLine()) != null)
{
Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
if(pattern.IsMatch(strToCheck))
{
//DO WHATEVER NOW
}
}
}I would appreciate any help. Thanks
// Find formats: dd/mm/yy or dd/mm/yyy, seperators: -/. (i.e. 31.01.08 31-01-08 etc.)
Regex rx = new Regex(@"([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.]((19|20)\d\d|\d\d)");// Define a test string.
string text = fileName;// Find matches.
MatchCollection matches = rx.Matches(text);// Report the number of matches found.
if (matches.Count > 1)
DoST();// Report on each match.
foreach (Match match in matches)
{
textBoxDocumentDate.Text = match.Value;
} -
Hi All, I need to compare a string to a regular expression to determine if the string is in this format. I am setting the string equal to a line read from a file. Once i have read the line i want to compare it to a regx to determine if the string is formatted as follows: Date Time (5-10 spaces) 123-12-123456 An example line: 09-02-2009 10:33 001-01-218913 The code i have currently is:
strFile = "C:\\test.txt";
using (StreamReader srTlog = new StreamReader(new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
while ((strLine = srTlog.ReadLine()) != null)
{
Regex pattern=new Regex("NOT SURE WHAT GOES HERE");
if(pattern.IsMatch(strToCheck))
{
//DO WHATEVER NOW
}
}
}I would appreciate any help. Thanks
Why go through the hassle of using regex? Just use
DateTime.ParseExact
, and handle thebool
return value."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001