Normal İfadelerdeki Miktar Niceleyiciler

Niceleyiciler, bir eşleşmenin bulunması için girişte karakter, grup veya karakter sınıfının kaç örneğinin bulunması gerektiğini belirtir. Aşağıdaki tabloda .NET tarafından desteklenen niceleyiciler listelenmektedir:

Doyumsuz niceleyici Yavaş niceleyici Açıklama
* *? Sıfır veya daha fazla kez eşleşir.
+ +? Bir veya daha fazla kez eşleşir.
? ?? Sıfır veya bir kez eşleşir.
{n} {n}? Tam olarak n kez eşleşir.
{n,} {n,}? En az n kez eşleşir.
{n,m} {n,m}? nile m saatleri arasında eşleşir.

ve m miktarları n tamsayı sabitleridir. Normalde niceleyiciler açgözlülüktür. Normal ifade altyapısının belirli desenlerin mümkün olduğunca çok oluşumuyla eşleşmesine neden olur. Karakterin ? bir niceleyiciye eklenmesi, karakterin yavaş olmasına neden olur. Normal ifade altyapısının mümkün olduğunca az oluşumla eşleşmesine neden olur. Doyumsuz ve tembel niceleyiciler arasındaki farkın tam açıklaması için bu makalenin devamında yer alan Açgözlü ve Tembel Niceleyiciler bölümüne bakın.

Önemli

Normal ifade deseni (a*)*gibi nicelleyicilerin iç içe yerleştirilmesi, normal ifade altyapısının gerçekleştirmesi gereken karşılaştırma sayısını artırabilir. Karşılaştırma sayısı, giriş dizesindeki karakter sayısının üstel işlevi olarak artabilir. Bu davranış ve geçici çözümleri hakkında daha fazla bilgi için bkz . Geri izleme.

Normal İfade Niceleyicileri

Aşağıdaki bölümlerde, .NET normal ifadeleri tarafından desteklenen niceleyiciler listelenmektedir:

Not

Normal ifade deseninde *, +, ?, {ve } karakterleriyle karşılaşılırsa, normal ifade altyapısı bunları bir karakter sınıfına dahil edilmedikleri sürece niceleyiciler veya niceleyici yapılarının bir parçası olarak yorumlar. Bunları bir karakter sınıfının dışındaki değişmez karakterler olarak yorumlamak için, bunların önüne ters eğik çizgi getirerek kaçış oluşturmanız gerekir. Örneğin, normal ifade desenindeki dize \* değişmez yıldız ("*") karakteri olarak yorumlanır.

Sıfır veya Daha Fazla Kez Eşleştir: *

Niceleyici, * önceki öğeyle sıfır veya daha fazla kez eşleşir. Niceleyiciye {0,} eşdeğerdir. * , yavaş eşdeğeri olan doyumsuz bir niceleyicidir *?.

Aşağıdaki örnekte bu normal ifade gösterilmektedir. Giriş dizesindeki dokuz basamak grubundan beşi desenle eşleşir ve dört (95, 929, 9219ve 9919) eşleşmez.

string pattern = @"\b91*9*\b";
string input = "99 95 919 929 9119 9219 999 9919 91119";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

// The example displays the following output:
//       '99' found at position 0.
//       '919' found at position 6.
//       '9119' found at position 14.
//       '999' found at position 24.
//       '91119' found at position 33.
Dim pattern As String = "\b91*9*\b"
Dim input As String = "99 95 919 929 9119 9219 999 9919 91119"
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '99' found at position 0.
'       '919' found at position 6.
'       '9119' found at position 14.
'       '999' found at position 24.
'       '91119' found at position 33.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Eşleşmenin bir sözcük sınırından başlaması gerektiğini belirtir.
91* 9 Sıfır veya daha fazla 1 karakterle eşleşir.
9* Sıfır veya daha fazla 9 karakterle eşleşir.
\b Eşleşmenin bir sözcük sınırında bitmesi gerektiğini belirtir.

Bir veya Daha Fazla Kez Eşleştir: +

Niceleyici, + önceki öğeyle bir veya daha fazla kez eşleşir. ile eşdeğerdir {1,}. + , yavaş eşdeğeri olan doyumsuz bir niceleyicidir +?.

Örneğin, normal ifade \ban+\w*?\b harfle başlayan sözcüklerin tamamını ve ardından harfin a bir veya daha fazla örneğini eşleştirmeye nçalışır. Aşağıdaki örnekte bu normal ifade gösterilmektedir. Normal ifade , , announcementannualve antiquesözcükleriyle aneşleşir ve ve allile autumn doğru eşleşmez.

string pattern = @"\ban+\w*?\b";

string input = "Autumn is a great time for an annual announcement to all antique collectors.";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

// The example displays the following output:
//       'an' found at position 27.
//       'annual' found at position 30.
//       'announcement' found at position 37.
//       'antique' found at position 57.
Dim pattern As String = "\ban+\w*?\b"

Dim input As String = "Autumn is a great time for an annual announcement to all antique collectors."
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'an' found at position 27.
'       'annual' found at position 30.
'       'announcement' found at position 37.
'       'antique' found at position 57.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
an+ Bir a veya daha fazla n karakterle eşleşir.
\w*? Bir sözcük karakteriyle sıfır veya daha fazla kez eşleşir, ancak mümkün olduğunca az kez.
\b Bir sözcük sınırında bit.

Sıfır veya Bir Kez Eşleştir: ?

Niceleyici, ? önceki öğeyle sıfır veya bir kez eşleşir. ile eşdeğerdir {0,1}. ? , yavaş eşdeğeri olan doyumsuz bir niceleyicidir ??.

Örneğin, normal ifade \ban?\b harfin ardından sıfır veya bir harf a örneğiyle başlayan sözcüklerin tamamını eşleştirmeye nçalışır. Başka bir deyişle ve ansözcükleriyle a eşleşmeye çalışır. Aşağıdaki örnekte bu normal ifade gösterilmektedir:

string pattern = @"\ban?\b";
string input = "An amiable animal with a large snout and an animated nose.";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

// The example displays the following output:
//        'An' found at position 0.
//        'a' found at position 23.
//        'an' found at position 42.
Dim pattern As String = "\ban?\b"
Dim input As String = "An amiable animal with a large snout and an animated nose."
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'An' found at position 0.
'       'a' found at position 23.
'       'an' found at position 42.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
an? a Ardından sıfır veya bir n karakterle eşleşir.
\b Bir sözcük sınırında bit.

Tam Olarak n Kez Eşleştir: {n}

n} niceleyici, {önceki öğeyle tam olarak n kez eşleşir; burada n herhangi bir tamsayıdır. {n, yavaş eşdeğeri n}? olan doyumsuz bir niceleyicidir{.}

Örneğin, normal ifade \b\d+\,\d{3}\b bir sözcük sınırını ve ardından bir veya daha fazla ondalık basamak ve ardından üç ondalık basamak ve ardından bir sözcük sınırı eşleştirmeyi dener. Aşağıdaki örnekte bu normal ifade gösterilmektedir:

string pattern = @"\b\d+\,\d{3}\b";
string input = "Sales totaled 103,524 million in January, " +
                      "106,971 million in February, but only " +
                      "943 million in March.";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        '103,524' found at position 14.
//        '106,971' found at position 45.
Dim pattern As String = "\b\d+\,\d{3}\b"
Dim input As String = "Sales totaled 103,524 million in January, " + _
                      "106,971 million in February, but only " + _
                      "943 million in March."
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '103,524' found at position 14.
'       '106,971' found at position 45.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
\d+ Bir veya daha fazla ondalık basamakla eşleşir.
\, Virgül karakteriyle eşleşir.
\d{3} Üç ondalık basamakla eşleşir.
\b Bir sözcük sınırında bit.

En Az n Kez Eşleştir: {n,}

{N,} niceleyici, önceki öğeyle en az n kez eşleşir; burada n herhangi bir tamsayıdır. {n, yavaş eşdeğeri n,}? olan doyumsuz bir niceleyicidir{.,}

Örneğin, normal ifade \b\d{2,}\b\D+ bir sözcük sınırıyla ve ardından en az iki basamakla ve ardından bir sözcük sınırı ve basamak olmayan bir karakterle eşleşmeye çalışır. Aşağıdaki örnekte bu normal ifade gösterilmektedir. Normal ifade, yalnızca bir ondalık basamak içerdiğinden, ancak ve "300 years"tümcecikleriyle "10 weeks" başarıyla eşleştiğinden tümcecikle eşleşemiyor"7 days".

string pattern = @"\b\d{2,}\b\D+";
string input = "7 days, 10 weeks, 300 years";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        '10 weeks, ' found at position 8.
//        '300 years' found at position 18.
Dim pattern As String = "\b\d{2,}\b\D+"
Dim input As String = "7 days, 10 weeks, 300 years"
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '10 weeks, ' found at position 8.
'       '300 years' found at position 18.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
\d{2,} En az iki ondalık basamakla eşleşir.
\b Bir sözcük sınırıyla eşleşir.
\D+ Ondalık olmayan en az bir basamakla eşleşir.

n ve m Saatleri Arasında Eşleştir: {n,m}

{n,m} niceleyicisi, önceki öğeyle en az n kez eşleşir, ancak en fazla m kez eşleşir; burada n ve m tamsayılardır. {n,m}, tembel eşdeğeri n,m}? olan doyumsuz bir niceleyicidir.{

Aşağıdaki örnekte, normal ifade (00\s){2,4} iki ile dört arasında iki sıfır basamak ve ardından bir boşluk arasındaki eşleşmeyi dener. Giriş dizesinin son bölümü, en fazla dört yerine beş kez bu deseni içerir. Ancak, bu alt dizenin yalnızca ilk kısmı (ara çubuğuna kadar ve beşinci sıfır çifti) normal ifade deseni ile eşleşir.

string pattern = @"(00\s){2,4}";
string input = "0x00 FF 00 00 18 17 FF 00 00 00 21 00 00 00 00 00";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        '00 00 ' found at position 8.
//        '00 00 00 ' found at position 23.
//        '00 00 00 00 ' found at position 35.
Dim pattern As String = "(00\s){2,4}"
Dim input As String = "0x00 FF 00 00 18 17 FF 00 00 00 21 00 00 00 00 00"
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '00 00 ' found at position 8.
'       '00 00 00 ' found at position 23.
'       '00 00 00 00 ' found at position 35.

Sıfır veya Daha Fazla Kez Eşleştir (Gecikmeli Eşleşme): *?

Niceleyici, *? önceki öğeyle sıfır veya daha fazla kez eşleşir ancak mümkün olduğunca az kez eşleşir. Doyumsuz niceleyicinin *tembel karşılığıdır.

Aşağıdaki örnekte, normal ifade \b\w*?oo\w*?\b dizesini ooiçeren tüm sözcüklerle eşleşir.

 string pattern = @"\b\w*?oo\w*?\b";
 string input = "woof root root rob oof woo woe";
 foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

 //  The example displays the following output:
//        'woof' found at position 0.
//        'root' found at position 5.
//        'root' found at position 10.
//        'oof' found at position 19.
//        'woo' found at position 23.
Dim pattern As String = "\b\w*?oo\w*?\b"
Dim input As String = "woof root root rob oof woo woe"
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'woof' found at position 0.
'       'root' found at position 5.
'       'root' found at position 10.
'       'oof' found at position 19.
'       'woo' found at position 23.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
\w*? Sıfır veya daha fazla sözcük karakteriyle eşleşir, ancak mümkün olduğunca az karakterle eşleşir.
oo dizesiyle ooeşleşir.
\w*? Sıfır veya daha fazla sözcük karakteriyle eşleşir, ancak mümkün olduğunca az karakterle eşleşir.
\b Sözcük sınırında sona erer.

Bir veya Daha Fazla Kez Eşleştir (Gecikmeli Eşleşme): +?

Niceleyici, +? önceki öğeyle bir veya daha fazla kez, ancak mümkün olduğunca az kez eşleşir. Doyumsuz niceleyicinin +tembel karşılığıdır.

Örneğin, normal ifade \b\w+?\b sözcük sınırlarıyla ayrılmış bir veya daha fazla karakterle eşleşir. Aşağıdaki örnekte bu normal ifade gösterilmektedir:

string pattern = @"\b\w+?\b";
string input = "Aa Bb Cc Dd Ee Ff";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'Aa' found at position 0.
//        'Bb' found at position 3.
//        'Cc' found at position 6.
//        'Dd' found at position 9.
//        'Ee' found at position 12.
//        'Ff' found at position 15.
Dim pattern As String = "\b\w+?\b"
Dim input As String = "Aa Bb Cc Dd Ee Ff"
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'Aa' found at position 0.
'       'Bb' found at position 3.
'       'Cc' found at position 6.
'       'Dd' found at position 9.
'       'Ee' found at position 12.
'       'Ff' found at position 15.

Sıfır veya Bir Kez Eşleştir (Gecikmeli Eşleşme): ??

Niceleyici, ?? önceki öğeyle sıfır veya bir kez eşleşir ancak mümkün olduğunca az kez eşleşir. Doyumsuz niceleyicinin ?tembel karşılığıdır.

Örneğin, normal ifade ^\s*(System.)??Console.Write(Line)??\(?? veya Console.WriteLinedizeleriyle Console.Write eşleşmeyi dener. Dize, öncesini Consolede içerebilir System. ve ardından bir açma parantezi eklenebilir. Dize bir satırın başında olmalıdır, ancak önünde boşluk olabilir. Aşağıdaki örnekte bu normal ifade gösterilmektedir:

string pattern = @"^\s*(System.)??Console.Write(Line)??\(??";
string input = "System.Console.WriteLine(\"Hello!\")\n" +
                      "Console.Write(\"Hello!\")\n" +
                      "Console.WriteLine(\"Hello!\")\n" +
                      "Console.ReadLine()\n" +
                      "   Console.WriteLine";
foreach (Match match in Regex.Matches(input, pattern,
                                      RegexOptions.IgnorePatternWhitespace |
                                      RegexOptions.IgnoreCase |
                                      RegexOptions.Multiline))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'System.Console.Write' found at position 0.
//        'Console.Write' found at position 36.
//        'Console.Write' found at position 61.
//        '   Console.Write' found at position 110.
Dim pattern As String = "^\s*(System.)??Console.Write(Line)??\(??"
Dim input As String = "System.Console.WriteLine(""Hello!"")" + vbCrLf + _
                      "Console.Write(""Hello!"")" + vbCrLf + _
                      "Console.WriteLine(""Hello!"")" + vbCrLf + _
                      "Console.ReadLine()" + vbCrLf + _
                      "   Console.WriteLine"
For Each match As Match In Regex.Matches(input, pattern, _
                                         RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase Or RegexOptions.MultiLine)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'System.Console.Write' found at position 0.
'       'Console.Write' found at position 36.
'       'Console.Write' found at position 61.
'       '   Console.Write' found at position 110.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
^ Giriş akışının başlangıcıyla eşleşir.
\s* Sıfır veya daha fazla boşluk karakteriyle eşleşir.
(System.)?? dizesinin System.sıfır veya bir oluşumuyla eşleşir.
Console.Write dizesiyle Console.Writeeşleşir.
(Line)?? dizesinin Linesıfır veya bir oluşumuyla eşleşir.
\(?? Açılış parantezinin sıfır veya bir oluşumuyla eşleşir.

Tam Olarak n Kez Eşleştir (Gecikmeli Eşleşme): {n}?

{n}? niceleyicisi, yukarıdaki öğeyle tam olarak n saat olarak eşleşir; burada n herhangi bir tamsayıdır. Doyumsuz niceleyici {n'nin} tembel karşılığıdır.

Aşağıdaki örnekte, bir web sitesi adresini tanımlamak için normal ifade \b(\w{3,}?\.){2}?\w{3,}?\b kullanılır. İfade ile eşleşir www.microsoft.com ancak msdn.microsoft.com veya mycompany.comile eşleşmiyormywebsite.

string pattern = @"\b(\w{3,}?\.){2}?\w{3,}?\b";
string input = "www.microsoft.com msdn.microsoft.com mywebsite mycompany.com";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'www.microsoft.com' found at position 0.
//        'msdn.microsoft.com' found at position 18.
Dim pattern As String = "\b(\w{3,}?\.){2}?\w{3,}?\b"
Dim input As String = "www.microsoft.com msdn.microsoft.com mywebsite mycompany.com"
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'www.microsoft.com' found at position 0.
'       'msdn.microsoft.com' found at position 18.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
(\w{3,}?\.) En az üç sözcük karakteriyle ancak mümkün olduğunca az karakterle eşleşir ve ardından nokta veya nokta karakteri eklenir. Bu desen, ilk yakalama grubudur.
(\w{3,}?\.){2}? İlk gruptaki deseni iki kez, ancak mümkün olduğunca az kez eşleştirir.
\b Eşleşmeyi bir sözcük sınırında sonlandırın.

En Az n Kez Eşleştir (Gecikmeli Eşleşme): {n,}?

{n,}? niceleyici, önceki öğeyle en az n kez eşleşir; burada n herhangi bir tamsayıdır ancak mümkün olduğunca az kez olur. Doyumsuz niceleyici {n'nin,} tembel karşılığıdır.

Bir çizim için {önceki bölümdeki n}? niceleyici örneğine bakın. Bu örnekteki normal ifade, en az üç karakter ve ardından nokta içeren bir dizeyle eşleştirmek için n,} niceleyicisini kullanır{.

n ile m Saatleri Arasında Eşleştir (Gecikmeli Eşleşme): {n,m}?

{n,m}? niceleyicisi ve saatleri arasında nm önceki öğeyle eşleşir; burada n ve m tamsayılardır ancak mümkün olduğunca az kez olur. Doyumsuz niceleyici {,n m'nin} tembel karşılığıdır.

Aşağıdaki örnekte, normal ifade \b[A-Z](\w*?\s*?){1,10}[.!?] 1 ile 10 arasında sözcük içeren cümlelerle eşleşir. 18 sözcük içeren bir tümce dışında giriş dizesindeki tüm cümlelerle eşleşir.

string pattern = @"\b[A-Z](\w*?\s*?){1,10}[.!?]";
string input = "Hi. I am writing a short note. Its purpose is " +
                      "to test a regular expression that attempts to find " +
                      "sentences with ten or fewer words. Most sentences " +
                      "in this note are short.";
foreach (Match match in Regex.Matches(input, pattern))
   Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'Hi.' found at position 0.
//        'I am writing a short note.' found at position 4.
//        'Most sentences in this note are short.' found at position 132.
Dim pattern As String = "\b[A-Z](\w*\s?){1,10}?[.!?]"
Dim input As String = "Hi. I am writing a short note. Its purpose is " + _
                      "to test a regular expression that attempts to find " + _
                      "sentences with ten or fewer words. Most sentences " + _
                      "in this note are short."
For Each match As Match In Regex.Matches(input, pattern)
    Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'Hi.' found at position 0.
'       'I am writing a short note.' found at position 4.
'       'Most sentences in this note are short.' found at position 132.

Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:

Desen Açıklama
\b Bir sözcük sınırında başla.
[A-Z] A ile Z arasında büyük harfli bir karakterle eşleşir.
(\w*?\s*?) Sıfır veya daha fazla sözcük karakteriyle eşleşir, ardından bir veya daha fazla boşluk karakteri eklenir ancak mümkün olduğunca az kez. Bu desen, ilk yakalama grubudur.
{1,10} Önceki deseni 1 ile 10 kez eşleştirir.
[.!?] , veya ?noktalama karakterlerinden .!herhangi biriyle eşleşir.

Doyumsuz ve Tembel Niceleyiciler

Bazı niceleyicilerin iki sürümü vardır:

  • Doyumsuz bir sürüm.

    Doyumsuz bir niceleyici, bir öğeyi mümkün olduğunca çok kez eşleştirmeye çalışır.

  • Doyumsuz olmayan (veya yavaş) bir sürüm.

    Doyumsuz olmayan bir niceleyici, bir öğeyi mümkün olduğunca az kez eşleştirmeye çalışır. Doyumsuz bir niceleyiciyi bir ekleyerek ?yavaş bir niceleyiciye dönüştürebilirsiniz.

Kredi kartı numarası gibi bir sayı dizesinden son dört basamağı ayıklamayı amaçlayan normal bir ifade düşünün. Doyumsuz niceleyiciyi kullanan normal ifadenin * sürümü şeklindedir \b.*([0-9]{4})\b. Ancak, bir dize iki sayı içeriyorsa, aşağıdaki örnekte gösterildiği gibi bu normal ifade yalnızca ikinci sayının son dört basamağını eşleştirir:

string greedyPattern = @"\b.*([0-9]{4})\b";
string input1 = "1112223333 3992991999";
foreach (Match match in Regex.Matches(input1, greedyPattern))
   Console.WriteLine("Account ending in ******{0}.", match.Groups[1].Value);

// The example displays the following output:
//       Account ending in ******1999.
Dim greedyPattern As String = "\b.*([0-9]{4})\b"
Dim input1 As String = "1112223333 3992991999"
For Each match As Match In Regex.Matches(input1, greedypattern)
    Console.WriteLine("Account ending in ******{0}.", match.Groups(1).Value)
Next
' The example displays the following output:
'       Account ending in ******1999.

Niceleyici, önceki öğeyi dizenin * tamamında mümkün olduğunca çok kez eşleştirmeye çalıştığından ve dizenin sonunda eşleşmesini bulduğundan, normal ifade ilk sayıyla eşleşemiyor.

Bu davranış istenen davranış değildir. Bunun yerine, aşağıdaki örnekte gösterildiği gibi her iki sayının basamaklarını ayıklamak için gecikmeli niceleyiciyi kullanabilirsiniz *? :

string lazyPattern = @"\b.*?([0-9]{4})\b";
string input2 = "1112223333 3992991999";
foreach (Match match in Regex.Matches(input2, lazyPattern))
   Console.WriteLine("Account ending in ******{0}.", match.Groups[1].Value);

// The example displays the following output:
//       Account ending in ******3333.
//       Account ending in ******1999.
Dim lazyPattern As String = "\b.*?([0-9]{4})\b"
Dim input2 As String = "1112223333 3992991999"
For Each match As Match In Regex.Matches(input2, lazypattern)
    Console.WriteLine("Account ending in ******{0}.", match.Groups(1).Value)
Next
' The example displays the following output:
'       Account ending in ******3333.
'       Account ending in ******1999.

Çoğu durumda, doyumsuz ve tembel niceleyicileri olan normal ifadeler aynı eşleşmeleri döndürür. Bunlar genellikle herhangi bir karakterle eşleşen joker karakter (.) meta karakterle kullanıldığında farklı sonuçlar döndürür.

Niceleyiciler ve Boş Eşleşmeler

, , +ve n,m} niceleyicileri *ve {bunların gecikmeli karşılıkları, en az yakalama sayısı bulunduğunda boş bir eşleşmeden sonra hiçbir zaman tekrarlamaz. Bu kural, olası grup yakalamalarının maksimum sayısı sonsuz veya sonsuza yakın olduğunda niceleyicilerin boş alt ifade eşleşmelerinde sonsuz döngüler girmesini engeller.

Örneğin, aşağıdaki kod, yöntemine Regex.Match yapılan çağrının sonucunu, sıfır veya bir karakter sıfır veya a daha fazla kez eşleşen normal ifade deseni (a?)*ile gösterir. Tek yakalama grubu ve aString.Emptydeğerlerini yakalar, ancak ikinci boş eşleşme yoktur çünkü ilk boş eşleşme niceleyicinin yinelemeyi durdurmasına neden olur.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(a?)*";
      string input = "aaabbb";
      Match match = Regex.Match(input, pattern);
      Console.WriteLine("Match: '{0}' at index {1}",
                        match.Value, match.Index);
      if (match.Groups.Count > 1) {
         GroupCollection groups = match.Groups;
         for (int grpCtr = 1; grpCtr <= groups.Count - 1; grpCtr++) {
            Console.WriteLine("   Group {0}: '{1}' at index {2}",
                              grpCtr,
                              groups[grpCtr].Value,
                              groups[grpCtr].Index);
            int captureCtr = 0;
            foreach (Capture capture in groups[grpCtr].Captures) {
               captureCtr++;
               Console.WriteLine("      Capture {0}: '{1}' at index {2}",
                                 captureCtr, capture.Value, capture.Index);
            }
         }
      }
   }
}
// The example displays the following output:
//       Match: 'aaa' at index 0
//          Group 1: '' at index 3
//             Capture 1: 'a' at index 0
//             Capture 2: 'a' at index 1
//             Capture 3: 'a' at index 2
//             Capture 4: '' at index 3
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "(a?)*"
        Dim input As String = "aaabbb"
        Dim match As Match = Regex.Match(input, pattern)
        Console.WriteLine("Match: '{0}' at index {1}",
                          match.Value, match.Index)
        If match.Groups.Count > 1 Then
            Dim groups As GroupCollection = match.Groups
            For grpCtr As Integer = 1 To groups.Count - 1
                Console.WriteLine("   Group {0}: '{1}' at index {2}",
                                  grpCtr,
                                  groups(grpCtr).Value,
                                  groups(grpCtr).Index)
                Dim captureCtr As Integer = 0
                For Each capture As Capture In groups(grpCtr).Captures
                    captureCtr += 1
                    Console.WriteLine("      Capture {0}: '{1}' at index {2}",
                                      captureCtr, capture.Value, capture.Index)
                Next
            Next
        End If
    End Sub
End Module
' The example displays the following output:
'       Match: 'aaa' at index 0
'          Group 1: '' at index 3
'             Capture 1: 'a' at index 0
'             Capture 2: 'a' at index 1
'             Capture 3: 'a' at index 2
'             Capture 4: '' at index 3

En az ve en fazla yakalama sayısını tanımlayan yakalama grubu ile sabit sayıda yakalama tanımlayan yakalama grubu arasındaki pratik farkı görmek için ve normal ifade desenlerini (a\1|(?(1)\1)){0,2}(a\1|(?(1)\1)){2}göz önünde bulundurun. Her iki normal ifade de aşağıdaki tabloda tanımlanan tek bir yakalama grubundan oluşur:

Desen Açıklama
(a\1 yakalanan ilk grubun değeriyle eşleşir a ...
|(?(1) ... veya yakalanan ilk grubun tanımlanıp tanımlanmadığını sınar. Yapısı (?(1) bir yakalama grubu tanımlamaz.
\1)) yakalanan ilk grup varsa, değerini eşleştirin. Grup yoksa, grubu ile eşleşecektir String.Empty.

İlk normal ifade bu deseni sıfır ile iki kez eşleştirmeye çalışır; ikinci, tam olarak iki kez. İlk desen, ilk yakalamasıyla minimum yakalama sayısına ulaştığından String.Empty, eşleşmeyi a\1denemek için hiçbir zaman tekrarlamaz. Niceleyici, {0,2} son yinelemede yalnızca boş eşleşmelere izin verir. Buna karşılık, ikinci normal ifade ikinci kez değerlendirdiğinden a\1 eşleşmiyora. En az yineleme sayısı olan 2, boş bir eşleşmeden sonra altyapıyı yinelemeye zorlar.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern, input;

      pattern = @"(a\1|(?(1)\1)){0,2}";
      input = "aaabbb";

      Console.WriteLine("Regex pattern: {0}", pattern);
      Match match = Regex.Match(input, pattern);
      Console.WriteLine("Match: '{0}' at position {1}.",
                        match.Value, match.Index);
      if (match.Groups.Count > 1) {
         for (int groupCtr = 1; groupCtr <= match.Groups.Count - 1; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
                              groupCtr, group.Value, group.Index);
            int captureCtr = 0;
            foreach (Capture capture in group.Captures) {
               captureCtr++;
               Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
                                 captureCtr, capture.Value, capture.Index);
            }
         }
      }
      Console.WriteLine();

      pattern = @"(a\1|(?(1)\1)){2}";
      Console.WriteLine("Regex pattern: {0}", pattern);
      match = Regex.Match(input, pattern);
         Console.WriteLine("Matched '{0}' at position {1}.",
                           match.Value, match.Index);
      if (match.Groups.Count > 1) {
         for (int groupCtr = 1; groupCtr <= match.Groups.Count - 1; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
                              groupCtr, group.Value, group.Index);
            int captureCtr = 0;
            foreach (Capture capture in group.Captures) {
               captureCtr++;
               Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
                                 captureCtr, capture.Value, capture.Index);
            }
         }
      }
   }
}
// The example displays the following output:
//       Regex pattern: (a\1|(?(1)\1)){0,2}
//       Match: '' at position 0.
//          Group: 1: '' at position 0.
//             Capture: 1: '' at position 0.
//
//       Regex pattern: (a\1|(?(1)\1)){2}
//       Matched 'a' at position 0.
//          Group: 1: 'a' at position 0.
//             Capture: 1: '' at position 0.
//             Capture: 2: 'a' at position 0.
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern, input As String

        pattern = "(a\1|(?(1)\1)){0,2}"
        input = "aaabbb"

        Console.WriteLine("Regex pattern: {0}", pattern)
        Dim match As Match = Regex.Match(input, pattern)
        Console.WriteLine("Match: '{0}' at position {1}.",
                          match.Value, match.Index)
        If match.Groups.Count > 1 Then
            For groupCtr As Integer = 1 To match.Groups.Count - 1
                Dim group As Group = match.Groups(groupCtr)
                Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
                                  groupCtr, group.Value, group.Index)
                Dim captureCtr As Integer = 0
                For Each capture As Capture In group.Captures
                    captureCtr += 1
                    Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
                                      captureCtr, capture.Value, capture.Index)
                Next
            Next
        End If
        Console.WriteLine()

        pattern = "(a\1|(?(1)\1)){2}"
        Console.WriteLine("Regex pattern: {0}", pattern)
        match = Regex.Match(input, pattern)
        Console.WriteLine("Matched '{0}' at position {1}.",
                          match.Value, match.Index)
        If match.Groups.Count > 1 Then
            For groupCtr As Integer = 1 To match.Groups.Count - 1
                Dim group As Group = match.Groups(groupCtr)
                Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
                                  groupCtr, group.Value, group.Index)
                Dim captureCtr As Integer = 0
                For Each capture As Capture In group.Captures
                    captureCtr += 1
                    Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
                                      captureCtr, capture.Value, capture.Index)
                Next
            Next
        End If
    End Sub
End Module
' The example displays the following output:
'       Regex pattern: (a\1|(?(1)\1)){0,2}
'       Match: '' at position 0.
'          Group: 1: '' at position 0.
'             Capture: 1: '' at position 0.
'       
'       Regex pattern: (a\1|(?(1)\1)){2}
'       Matched 'a' at position 0.
'          Group: 1: 'a' at position 0.
'             Capture: 1: '' at position 0.
'             Capture: 2: 'a' at position 0.

Ayrıca bkz.