Hi All,
Can anyone suggest me an RegularExpression which validates a string like below.What i am trying to do is to validate an datetime filter provided by user.
InputDate: {operator}{Date}{operator}{Date}{operator}.........{date} There arises many cases eg: inputdate: >12/08/2009 or <12/09/2009 and != 13/08/2009 .. similar to this Do we any regular expression to do this. | | nothingisimpossible | Actually, I see.... inputdate: {operator}{date}{operator}{operator}{date}{operator}{operator}{date}...
which would be... inputdate: {compare operator}{date}({bool operator}{compare operator}{date})*
which makes your pattern look something like this...
inputdate:(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4})((?<boolOp>\s*(and|or)\s*)(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4}))*
Maybe this C# example will help clarify...
string pattern = @"inputdate:
(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4})
((?<boolOp>\s*(and|or)\s*)(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4}))*";
string test = "inputdate: >12/08/2009 or <12/09/2009 and != 13/08/2009";
foreach (Match mx in Regex.Matches(test, pattern,RegexOptions.IgnorePatternWhitespace))
{
for (int idx = 0; idx < mx.Groups["compOp"].Captures.Count; ++idx)
{
Console.WriteLine("{0}: {1}: {2}",
(idx == 0) ? "(none)" : mx.Groups["boolOp"].Captures[idx-1].Value,
mx.Groups["compOp"].Captures[idx].Value,
mx.Groups["date"].Captures[idx].Value);
}
}
Les Potter, Xalnix Corporation, Yet Another C# Blog- Marked As Answer bynothingisimpossible Friday, September 11, 2009 2:03 PM
- Proposed As Answer byJohnGrove Thursday, September 10, 2009 7:38 PM
-
| | xalnix | Try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String pattern = @"^(>|<|!=|=)?\s?(?<Month>\d{1,2})\/
(?<Day>\d{1,2})\/
(?<Year>(?:\d{4}|\d{2}))";
String[] testWords = {">12/08/2009", "<12/09/2009", "!=13/08/2009",
"123/90/llll", "kojm", "12"};
Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
foreach (String word in testWords)
{
Match m = rx.Match(word);
if (m.Success)
Console.WriteLine("Success: {0}", m.Value);
else
Console.WriteLine("Failed: {0}", word);
}
Console.ReadLine();
}
}
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Use the regular expression as below:
Dim wordsmatchcoll As MatchCollection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
wordsmatchcoll = Regex.Matches(RichTextBox1.Text, "(((([<>=])|([!])(=))(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3})))|((([<>=])|([!])(=))(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3})))|((([<>=])|([!])(=))(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3})))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([2468][048]00))|((0?2)([/])(29)([/])([3579][26]00))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([1][89][0][48]))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([2-9][0-9][0][48]))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([1][89][2468][048]))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([2-9][0-9][2468][048]))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([1][89][13579][26]))|((([<>=])|([!])(=))(0?2)([/])(29)([/])([2-9][0-9][13579][26])))", RegexOptions.IgnoreCase)
If wordsmatchcoll.Count > 0 Then
MsgBox("Has Values")
Else
MsgBox("no matches")
End If
Dim match1 As Match
For Each match1 In wordsmatchcoll
MsgBox(match1.Value)
Next
End Sub
| | Rohini Chavakula | What is the real goal? If you want validate the actual date, extract it via regex sure; but use the DateTime.Parse to validate it against the user's current country/culture settings on the PC.
William Wegerson ( www.OmegaCoder.Com ) | | OmegaMan | Rohini, It took me 10 minutes just to look through your regex.
I would not "recommend" that..
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Actually, I see.... inputdate: {operator}{date}{operator}{operator}{date}{operator}{operator}{date}...
which would be... inputdate: {compare operator}{date}({bool operator}{compare operator}{date})*
which makes your pattern look something like this...
inputdate:(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4})((?<boolOp>\s*(and|or)\s*)(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4}))*
Maybe this C# example will help clarify...
string pattern = @"inputdate:
(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4})
((?<boolOp>\s*(and|or)\s*)(?<compOp>\s*([<>]|==|!=)\s*)(?<date>\d{2}/\d{2}/\d{4}))*";
string test = "inputdate: >12/08/2009 or <12/09/2009 and != 13/08/2009";
foreach (Match mx in Regex.Matches(test, pattern,RegexOptions.IgnorePatternWhitespace))
{
for (int idx = 0; idx < mx.Groups["compOp"].Captures.Count; ++idx)
{
Console.WriteLine("{0}: {1}: {2}",
(idx == 0) ? "(none)" : mx.Groups["boolOp"].Captures[idx-1].Value,
mx.Groups["compOp"].Captures[idx].Value,
mx.Groups["date"].Captures[idx].Value);
}
}
Les Potter, Xalnix Corporation, Yet Another C# Blog- Marked As Answer bynothingisimpossible Friday, September 11, 2009 2:03 PM
- Proposed As Answer byJohnGrove Thursday, September 10, 2009 7:38 PM
-
| | xalnix | Don't know what you want. Regex can validate user input text, it can expell invalid date like13/32/9999
But if you have other striction such as later than some day, you'd better compare them in date type. www.wonderstudio.cn | | Eping Wang |
|