Capture.Value Özellik
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Yakalanan alt dizeyi giriş dizesinden alır.
public:
property System::String ^ Value { System::String ^ get(); };
public string Value { get; }
member this.Value : string
Public ReadOnly Property Value As String
Özellik Değeri
Eşleşme tarafından yakalanan alt dize.
Örnekler
Aşağıdaki örnek, nokta (".") dışında noktalama işareti içermeyen tümcelerle eşleşen bir normal ifade tanımlar.
Match.Value özelliği, her eşleşme için eşleşen bir tümceden oluşan sonuç dizesini görüntüler.
Group.Value özelliği her yakalama grubu için sonuç dizesini görüntüler; bu yakalama grubu tarafından yakalanan son dizeden oluşur. özelliği, Capture.Value her yakalama için sonuç dizesini görüntüler.
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "Yes. This dog is very friendly.";
string pattern = @"((\w+)[\s.])+";
foreach (Match match in Regex.Matches(input, pattern))
{
Console.WriteLine("Match: {0}", match.Value);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Group group = match.Groups[groupCtr];
Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value);
for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: {1}", captureCtr,
group.Captures[captureCtr].Value);
}
}
}
}
// The example displays the following output:
// Match: Yes.
// Group 0: Yes.
// Capture 0: Yes.
// Group 1: Yes.
// Capture 0: Yes.
// Group 2: Yes
// Capture 0: Yes
// Match: This dog is very friendly.
// Group 0: This dog is very friendly.
// Capture 0: This dog is very friendly.
// Group 1: friendly.
// Capture 0: This
// Capture 1: dog
// Capture 2: is
// Capture 3: very
// Capture 4: friendly.
// Group 2: friendly
// Capture 0: This
// Capture 1: dog
// Capture 2: is
// Capture 3: very
// Capture 4: friendly
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim input As String = "Yes. This dog is very friendly."
Dim pattern As String = "((\w+)[\s.])+"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("Match: {0}", match.Value)
For groupCtr As Integer = 0 To match.Groups.Count - 1
Dim group As Group = match.Groups(groupCtr)
Console.WriteLine(" Group {0}: {1}", groupCtr, group.Value)
For captureCtr As Integer = 0 To group.Captures.Count - 1
Console.WriteLine(" Capture {0}: {1}", captureCtr, _
group.Captures(captureCtr).Value)
Next
Next
Next
End Sub
End Module
' The example displays the following output:
' Match: Yes.
' Group 0: Yes.
' Capture 0: Yes.
' Group 1: Yes.
' Capture 0: Yes.
' Group 2: Yes
' Capture 0: Yes
' Match: This dog is very friendly.
' Group 0: This dog is very friendly.
' Capture 0: This dog is very friendly.
' Group 1: friendly.
' Capture 0: This
' Capture 1: dog
' Capture 2: is
' Capture 3: very
' Capture 4: friendly.
' Group 2: friendly
' Capture 0: This
' Capture 1: dog
' Capture 2: is
' Capture 3: very
' Capture 4: friendly
Normal ifade deseni ((\w+)[\s.])+ aşağıdaki tabloda gösterildiği gibi tanımlanır. Bu normal ifadede, normal ifadenin tamamına bir nicel (+) uygulandığını unutmayın.
| Desen | Açıklama |
|---|---|
(\w+) |
Bir veya daha fazla sözcük karakteri eşleştir. Bu ikinci yakalama grubudur. |
[\s.]) |
Boşluk karakterini veya nokta (".") eşleştirin. |
((\w+)[\s.]) |
Bir veya daha fazla sözcük karakterini ve ardından boşluk karakteri veya nokta (".") eşleştirin. İlk yakalama grubu budur. |
((\w+)[\s.])+ |
Bir sözcük karakterinin veya karakterin bir veya daha fazla tekrarını, ardından boşluk karakteri veya nokta (".") ile eşleştirin. |
Bu örnekte, giriş dizesi iki cümleden oluşur. Çıktıda gösterildiği gibi, ilk tümce yalnızca bir sözcük içerir, bu nedenle CaptureCollection nesnenin nesneyle aynı yakalamayı Group temsil eden tek Capture bir nesnesi vardır. İkinci tümce birden çok sözcük içerdiğinden, Group nesneler yalnızca son eşleşen alt ifade hakkındaki bilgileri içerir. İlk yakalamayı temsil eden Grup 1, cümlede kapanış dönemi olan son sözcüğü içerir. İkinci yakalamayı temsil eden Grup 2, tümcedeki son sözcüğü içerir. Ancak, Capture grubun CaptureCollection nesnesindeki nesneler her alt ifade eşleşmesini yakalar. Capture İlk yakalama grubunun yakalama koleksiyonundaki nesneler, yakalanan her sözcük ve boşluk karakteri veya nokta hakkında bilgi içerir. Capture İkinci yakalama grubunun yakalama koleksiyonundaki nesneler, yakalanan her sözcük hakkında bilgi içerir.
Aşağıdaki örnekte, ^([a-z]+)(\d+)*\.([a-z]+(\d)*)$noktayla ayrılmış iki bölümden oluşan bir ürün numarasıyla eşleştirmek için normal ifade deseni kullanılır. Her iki bölüm de alfabetik karakterlerden ve ardından isteğe bağlı sayılardan oluşur. İlk giriş dizesi desenle eşleşmediğinden, döndürülen System.Text.RegularExpressions.Match nesnenin Value özelliğinin değeri olur String.Empty. Benzer şekilde, normal ifade deseni bir yakalama grubuyla eşleşemediğinde, ilgili Group nesnenin Value özelliğinin değeri olur String.Empty.
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
String pattern = @"^([a-z]+)(\d+)?\.([a-z]+(\d)*)$";
String[] values = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" };
foreach (var value in values) {
Match m = Regex.Match(value, pattern, RegexOptions.IgnoreCase);
if (m.Success) {
Console.WriteLine("Match: '{0}'", m.Value);
Console.WriteLine(" Number of Capturing Groups: {0}",
m.Groups.Count);
for (int gCtr = 0; gCtr < m.Groups.Count; gCtr++) {
Group group = m.Groups[gCtr];
Console.WriteLine(" Group {0}: {1}", gCtr,
group.Value == "" ? "<empty>" : "'" + group.Value + "'");
Console.WriteLine(" Number of Captures: {0}",
group.Captures.Count);
for (int cCtr = 0; cCtr < group.Captures.Count; cCtr++)
Console.WriteLine(" Capture {0}: {1}",
cCtr, group.Captures[cCtr].Value);
}
}
else {
Console.WriteLine("No match for {0}: Match.Value is {1}",
value, m.Value == String.Empty ? "<empty>" : m.Value);
}
}
}
}
// The example displays the following output:
// No match for AC10: Match.Value is <empty>
// Match: 'Za203.CYM'
// Number of Capturing Groups: 5
// Group 0: 'Za203.CYM'
// Number of Captures: 1
// Capture 0: Za203.CYM
// Group 1: 'Za'
// Number of Captures: 1
// Capture 0: Za
// Group 2: '203'
// Number of Captures: 1
// Capture 0: 203
// Group 3: 'CYM'
// Number of Captures: 1
// Capture 0: CYM
// Group 4: <empty>
// Number of Captures: 0
// Match: 'XYZ.CoA'
// Number of Capturing Groups: 5
// Group 0: 'XYZ.CoA'
// Number of Captures: 1
// Capture 0: XYZ.CoA
// Group 1: 'XYZ'
// Number of Captures: 1
// Capture 0: XYZ
// Group 2: <empty>
// Number of Captures: 0
// Group 3: 'CoA'
// Number of Captures: 1
// Capture 0: CoA
// Group 4: <empty>
// Number of Captures: 0
// Match: 'ABC.x170'
// Number of Capturing Groups: 5
// Group 0: 'ABC.x170'
// Number of Captures: 1
// Capture 0: ABC.x170
// Group 1: 'ABC'
// Number of Captures: 1
// Capture 0: ABC
// Group 2: <empty>
// Number of Captures: 0
// Group 3: 'x170'
// Number of Captures: 1
// Capture 0: x170
// Group 4: '0'
// Number of Captures: 3
// Capture 0: 1
// Capture 1: 7
// Capture 2: 0
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "^([a-z]+)(\d+)?\.([a-z]+(\d)*)$"
Dim values() As String = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" }
For Each value In values
Dim m As Match = Regex.Match(value, pattern, RegexOptions.IgnoreCase)
If m.Success Then
Console.WriteLine("Match: '{0}'", m.Value)
Console.WriteLine(" Number of Capturing Groups: {0}",
m.Groups.Count)
For gCtr As Integer = 0 To m.Groups.Count - 1
Dim group As Group = m.Groups(gCtr)
Console.WriteLine(" Group {0}: {1}", gCtr,
If(group.Value = "", "<empty>", "'" + group.Value + "'"))
Console.WriteLine(" Number of Captures: {0}",
group.Captures.Count)
For cCtr As Integer = 0 To group.Captures.Count - 1
Console.WriteLine(" Capture {0}: {1}",
cCtr, group.Captures(cCtr).Value)
Next
Next
Else
Console.WriteLine("No match for {0}: Match.Value is {1}",
value, If(m.Value = String.Empty, "<empty>", m.Value))
End If
Next
End Sub
End Module
' The example displays the following output:
' No match for AC10: Match.Value is <empty>
' Match: 'Za203.CYM'
' Number of Capturing Groups: 5
' Group 0: 'Za203.CYM'
' Number of Captures: 1
' Capture 0: Za203.CYM
' Group 1: 'Za'
' Number of Captures: 1
' Capture 0: Za
' Group 2: '203'
' Number of Captures: 1
' Capture 0: 203
' Group 3: 'CYM'
' Number of Captures: 1
' Capture 0: CYM
' Group 4: <empty>
' Number of Captures: 0
' Match: 'XYZ.CoA'
' Number of Capturing Groups: 5
' Group 0: 'XYZ.CoA'
' Number of Captures: 1
' Capture 0: XYZ.CoA
' Group 1: 'XYZ'
' Number of Captures: 1
' Capture 0: XYZ
' Group 2: <empty>
' Number of Captures: 0
' Group 3: 'CoA'
' Number of Captures: 1
' Capture 0: CoA
' Group 4: <empty>
' Number of Captures: 0
' Match: 'ABC.x170'
' Number of Capturing Groups: 5
' Group 0: 'ABC.x170'
' Number of Captures: 1
' Capture 0: ABC.x170
' Group 1: 'ABC'
' Number of Captures: 1
' Capture 0: ABC
' Group 2: <empty>
' Number of Captures: 0
' Group 3: 'x170'
' Number of Captures: 1
' Capture 0: x170
' Group 4: '0'
' Number of Captures: 3
' Capture 0: 1
' Capture 1: 7
' Capture 2: 0
Normal ifade deseni aşağıdaki tabloda gösterildiği gibi tanımlanır:
| Desen | Açıklama |
|---|---|
^ |
Eşleşmeye dizenin başında başlayın. |
([a-z]+) |
Bir karakterden z'ye kadar herhangi bir karakterin bir veya daha fazla örneğini eşleştirin. Normal ifade altyapısı seçeneğine geçirildiğinden RegexOptions.IgnoreCase , bu karşılaştırma büyük/küçük harfe duyarlı değildir. İlk yakalama grubu budur. |
(\d+)? |
Bir veya daha fazla ondalık basamağın sıfır veya bir oluşumunu eşleştirin. Bu ikinci yakalama grubudur. |
\. |
Değişmez nokta karakterini eşleştirin. |
([a-z]+ |
Bir karakterden z'ye kadar herhangi bir karakterin bir veya daha fazla örneğini eşleştirin. Karşılaştırma büyük/küçük harfe duyarlı değildir. |
(\d)* |
Sıfır veya daha fazla ondalık basamağı eşleştirin. Eşleşen tek bir basamak dördüncü yakalama grubudur. |
([a-z]+(\d)*) |
Bir veya daha fazla alfabetik karakterden z'ye ve ardından sıfır, bir veya daha fazla ondalık basamak eşleştirin. Bu dördüncü yakalama grubudur. |
$ |
Dizenin sonundaki eşleşmeyi sonlandırın. |
Açıklamalar
veya yöntemine yapılan Regex.Match bir çağrı eşleşme bulamazsa, döndürülen Match.Value özelliğin değeri olurString.Empty.Match.NextMatch Normal ifade altyapısı bir yakalama grubuyla eşleşemiyorsa. döndürülen Group.Value özelliğin değeri şeklindedir String.Empty. Çizim için ikinci örne bakın.