是: (请说明导致实际问题的长期包裹)
I m 允许用户使用简单的日期/时间尺度迅速进入日期(不需要日期选择)
for example they can enter:
d +1d -2h
this will give them a date time string of todays s date, plus one day, minus two hours.
anyways I ve created a regex to match these which works fine (probably not the best way to do it but it works!):
[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*
由于你可能用reg子对一米进行猜测,以便在分类以计算结果的日期之前验证这些条目。 首先,我使用了类似的东西:
Regex rgxDateTimeMacro = new Regex(@"[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*");
if(rgxDateTimeMacro.isMatch(strInput)){
...string passes...
}
I then quickly realised that isMatch returns true if there s any matches in the passed string,
d +1d +1
would return true ^__^
这样一来,它就改变,以做这样的事情:
Regex rgxDateTimeMacro = new Regex(@"[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*");
MatchCollection objMatches = rgxDateTimeMacro.Matches(strInput);
if (objMatches.Count > 0)
{
// to pass.. we need a match which is the same length as the input string...
foreach (Match m in objMatches)
{
if (m.Length == strInput.Length)
{
...string passes...
}
}
}
现在这项工作是很出色的,但我的问题是:是否有更简单的方法来检查一下一下(whole string)是否与reg吻合? 我的洞.,但似乎找不到明显的答案。
这种希望是有意义的。
Pete
附录
由于所有快速答案,$美元确实是trick的:
(s_^)