簡體   English   中英

誰能告訴我以下正則表達式為何與“ 2015”不匹配?

[英]Can anyone tell me why the following regex won't match to “2015”?

string str = @"(?<Year>\d{4})?";
Regex regex = new Regex(str);
var match = regex.Match("abc/2015/01/11/efg_20150111.tsv");

我在match.Groups中找不到“ 2015”。 謝謝!

那是因為您的正則表達式是可選的。 (?<Year>\\d{4})? 說,“匹配4個連續的數字, 或者什么都不匹配。沒有什么是匹配的了,正如一個小型測試程序所演示的:

Regex  rx = new Regex( @"(?<Year>\d{4})?");
Match m = rx.Match("abc/2015/01/11/efg_20150111.tsv");
int i = 0 ;

while ( m.Success)
{
  Console.WriteLine( "match #{0}: +{1}({2}) is '{3}'." ,
    ++i , m.Index , m.Length , m.Value ) ;
  m = m.NextMatch();
}
if ( i == 0 )
{
  Console.WriteLine( "No matches");
}

上面的代碼在輸出中顯示的任何地方都可以找到零長度匹配:

match #1: +0(0) is ''.
match #2: +1(0) is ''.
match #3: +2(0) is ''.
match #4: +3(0) is ''.
match #5: +4(4) is '2015'.
match #6: +8(0) is ''.
match #7: +9(0) is ''.
match #8: +10(0) is ''.
match #9: +11(0) is ''.
match #10: +12(0) is ''.
match #11: +13(0) is ''.
match #12: +14(0) is ''.
match #13: +15(0) is ''.
match #14: +16(0) is ''.
match #15: +17(0) is ''.
match #16: +18(0) is ''.
match #17: +19(4) is '2015'.
match #18: +23(4) is '0111'.
match #19: +27(0) is ''.
match #20: +28(0) is ''.
match #21: +29(0) is ''.
match #22: +30(0) is ''.
match #23: +31(0) is ''.

這就是正則表達式引擎的工作方式。

不要將命名捕獲組設為可選。

@"(?<=\/)(?<Year>\d{4})(?=\/)"

要么

@"\b(?<Year>\d{4})\b"

演示

(?<Year>\\d{4})? 匹配可選的4位數字。 ? 在指定的捕獲組之后,將整個組設為可選。 並添加單詞邊界,以便從20150111不會使用4位數字

string input = "abc/2015/01/11/efg_20150111.tsv";
Regex rgx = new Regex(@"\b(?<Year>\d{4})\b");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[1].Value);

愛迪生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM