English 中文(简体)
。 NET Regex isMatch vsmates questions
原标题:.NET Regex isMatch vs Matches question
  • 时间:2009-11-18 12:44:23
  •  标签:
  • c#
  • .net
  • regex

是: (请说明导致实际问题的长期包裹)

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_^)

最佳回答

如果我正确理解,使用:^my regex$

^ - Start of the string
$ - End of the string

问题回答

书写一种更好的模式,只有与你真正想要的相匹配。

我的建议;

∗∗∗∗∗∗∗

说明差异的简短解释;

1: ^                  beginning of string
2: [DTdt]             matches 1 character of the given
3:  (                 open group 1
4:   s+              one or more whitespaces
5:   [+-]             either + or -
6:   s+              see above
7:   [1-9][0-9]*      matches one number of 9 followed by none or more numbers of 10
8:   [dDhHmMwW]       one of the characters
9:  )                 close group 1
10: +                 let group 1 only repeat 1 or more times
11: $                 end of string

我希望你看到你模式的差异。

Matches: D +19d, t -99w +14d, T +75m -64H, D -1d +4m -44h

No Matches: d, d +1, T +1H -2, +1D -5M, -134d, t-4m, t +5d5, D -3m-5d+3g

在常规表述中,您可使用<代码>>>^和<$等特性表示该表述应与整个测试范围一致:

  • ^ as the very first character means "Match from the start of my test string".
  • $ as the very last character means "Match to the end of my test string".

合并(例如^abc$) 整个试验舱必须与表述一致。

利用这一编号:

"^[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*$"

胎体的特性与胎体的开始相吻合,而费用是指胎体的结束。

使用:<代码>Abla

  1. A Beginning of subject.
  2.  End of subject.

或使用:^bla$,多行式。

  1. ^ Beginning of a line.
  2. $ End of a line.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签