Sideway
output.to from Sideway


Built-in Componet


Regular Expression




Draft for Information Only

Content

Regular Expression Result
 Match Class
  Properties
  Methods
  Remarks
  Applies to
  See also
 MatchCollection Class
  Properties
  Methods
  Extension Methods
  Remarks
  Applies to
 Examples
 Source/Reference

Regular Expression Result

The results of the regular expression engine of .NET framework can be accessed by Capture Class, CaptureCollection Class, Group Class, GroupCollection Class, Match Class, MatchCollection Class. Both Match Class and MatchCollection Class are used to represent the object return by the Match Method and Matches Method of Regex Class respectively.

Match Class

Represents the results from a single regular expression match.

NamespaceSystem.Text.RegularExpressions AssembliesSystem.Text.RegularExpressions.dll, System.dll, netstandard.dll
[System.Serializable]
public class Match : System.Text.RegularExpressions.Group

Properties

Captures Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RightToLeft option). The collection may have zero or more items. (Inherited from Group)
Empty Gets the empty group. All failed matches return this empty match.
Groups Gets a collection of groups matched by the regular expression.
Index The position in the original string where the first character of the captured substring is found. (Inherited from Capture)
Length Gets the length of the captured substring. (Inherited from Capture)
Name Returns the name of the capturing group represented by the current instance. (Inherited from Group)
Success Gets a value indicating whether the match is successful. (Inherited from Group)
Value Gets the captured substring from the input string. (Inherited from Capture)

Methods

Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object)
GetHashCode() Serves as the default hash function. (Inherited from Object)
GetType() Gets the Type of the current instance. (Inherited from Object)
MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object)
NextMatch() Returns a new Match object with the results for the next match, starting at the position at which the last match ended (at the character after the last matched character).
Result(String) Returns the expansion of the specified replacement pattern.
Synchronized(Match) Returns a Match instance equivalent to the one supplied that is suitable to share between multiple threads.
ToString() Retrieves the captured substring from the input string by calling the Value property. (Inherited from Capture)

Remarks

  • The Match object is immutable and has no public constructor. An instance of the Match class is returned by the Regex.Match method and represents the first pattern match in a string. Subsequent matches are represented by Match objects returned by the Match.NextMatch method. In addition, a MatchCollection object that consists of zero, one, or more Match objects is returned by the Regex.Matches method.

    If the Regex.Matches method fails to match a regular expression pattern in an input string, it returns an empty MatchCollection object. You can then use a foreach construct in C# or a For Each construct in Visual Basic to iterate the collection.

    If the Regex.Match method fails to match the regular expression pattern, it returns a Match object that is equal to Match.Empty. You can use the Success property to determine whether the match was successful.
  • If a pattern match is successful, the Value property contains the matched substring, the Index property indicates the zero-based starting position of the matched substring in the input string, and the Length property indicates the length of matched substring in the input string.

    Because a single match can involve multiple capturing groups, Match has a Groups property that returns the GroupCollection. The Match instance itself is equivalent to the first object in the collection, at Match.Groups[0] (Match.Groups(0) in Visual Basic), which represents the entire match. You can access the captured groups in a match in the following ways:

    • You can iterate the members of the GroupCollection object by using a foreach (C#) or For Each (Visual Basic) construct.
    • You can use the GroupCollection.Item[Int32] property to retrieve groups by the number of the capturing group. Note that you can determine which numbered groups are present in a regular expression by calling the instance Regex.GetGroupNumbers method.
    • You can use the GroupCollection.Item[String] property to retrieve groups by the name of the capturing group. Note that you can determine which named groups are present in a regular expression by calling the instance Regex.GetGroupNames() method.

Applies to

.NET Framework: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

See also

  • MatchCollection
  • Regex

MatchCollection Class

Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.

NamespaceSystem.Text.RegularExpressions AssembliesSystem.Text.RegularExpressions.dll, System.dll, netstandard.dll
[System.Serializable]
public class MatchCollection : System.Collections.ICollection

Properties

Count Gets the number of matches.
IsReadOnly Gets a value that indicates whether the collection is read only.
IsSynchronized Gets a value indicating whether access to the collection is synchronized (thread-safe).
Item[Int32] Gets an individual member of the collection.
SyncRoot Gets an object that can be used to synchronize access to the collection.

Methods

CopyTo(Array, Int32) Copies all the elements of the collection to the given array starting at the given index.
Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object)
GetEnumerator() Provides an enumerator that iterates through the collection.
GetHashCode() Serves as the default hash function. (Inherited from Object)
GetType() Gets the Type of the current instance. (Inherited from Object)
MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object)
ToString() Returns a string that represents the current object. (Inherited from Object)

Extension Methods

Cast<TResult>(IEnumerable) Casts the elements of an IEnumerable to the specified type.
OfType<TResult>(IEnumerable) Filters the elements of an IEnumerable based on a specified type.
AsParallel(IEnumerable) Enables parallelization of a query.
AsQueryable(IEnumerable) Converts an IEnumerable to an IQueryable.

Remarks

  • The collection is immutable (read-only) and has no public constructor. The Regex.Matches method returns a MatchCollection object.

    The collection contains zero or more System.Text.RegularExpressions.Match objects. If the match is successful, the collection is populated with one System.Text.RegularExpressions.Match object for each match found in the input string. If the match is unsuccessful, the collection contains no System.Text.RegularExpressions.Match objects, and its Count property equals zero.

    When applying a regular expression pattern to a particular input string, the regular expression engine uses either of two techniques to build the MatchCollection object:

    • Direct evaluation. The MatchCollection object is populated all at once, with all matches resulting from a particular call to the Regex.Matches method. This technique is used when the collection's Count property is accessed. It typically is the more expensive method of populating the collection and entails a greater performance hit.
    • Lazy evaluation. The MatchCollection object is populated as needed on a match-by-match basis. It is equivalent to the regular expression engine calling the Regex.Match method repeatedly and adding each match to the collection. This technique is used when the collection is accessed through its GetEnumerator method, or when it is accessed using the foreach statement (in C#) or the For Each...Next statement (in Visual Basic).
    To iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For Each…Next in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

Applies to

.NET Framework: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

Examples

Examples of Match, and MatchCollection Classes

Vb.NET ASPX File:
<%@ Page Language="VB" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <script runat="server">
           Sub Page_Load()
               Dim xpattern As String = "\w+"
               Dim xstring As String = "01234 67 901 34567 9012 4567890"
               Dim xRegex As Regex = New Regex(xpattern, RegexOptions.IgnoreCase)
               Dim xmatch As Match
               Dim xmatches As MatchCollection
               Dim xmatchstr As String
               xmatchstr = "Given pattern: """ & xpattern & """<br />"
               xmatchstr = xmatchstr & "Given string: """ & xstring & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string): """
               xmatch = xRegex.Match(xstring)
               xmatchstr = xmatchstr & xmatch.value & """<br />"
               xmatchstr = xmatchstr & "Result of Match.Captures.Count: """
               xmatchstr = xmatchstr & xmatch.Captures.Count & """<br />"
               xmatchstr = xmatchstr & "Result of Match.Groups.Count: """
               xmatchstr = xmatchstr & xmatch.Groups.Count & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string): """
               xmatchstr = xmatchstr & xRegex.Match(xstring).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,4): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,4).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,5): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,5).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,6): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,6).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,7): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,7).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,8,3): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,8,3).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,9,3): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,9,3).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,10,3): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,10,3).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,11,3): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,11,3).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,12,3): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,12,3).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,13,3): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,13,3).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Match(string,10,7): """
               xmatchstr = xmatchstr & xRegex.Match(xstring,10,7).value & """<br />"
               xmatchstr = xmatchstr & "Result of Regex.Matches(string,pattern).Count: """
               xmatches = Regex.Matches(xstring,xpattern)
               xmatchstr = xmatchstr & xmatches.Count & """<br />"
               Dim xint As Integer = 0
               xmatchstr = xmatchstr & "Result of Regex.Matches(string,pattern)(xint): ""Value, Index, Length""<br />"
               For Each xmatch in xmatches
               xmatchstr = xmatchstr & "Result of Regex.Matches(string,pattern)("& xint & "): """
               xmatchstr = xmatchstr & xmatch.Value & ", " & xmatch.Index & ", " & xmatch.Length & """<br />"
               xint = xint + 1
               Next
               lbl01.Text = xmatchstr
           End Sub
       </script>
    </head>
    <body>
       <% Response.Write ("<h1>This is a Sample Page of Match, and MatchCollection Classes</h1>") %>
       <p>
           <%-- Set on Page_Load --%>
           <asp:Label id="lbl01" runat="server" />
       </p>
    </body>
</html>
HTML Web Page Embedded Output:

Source/Reference

  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capture?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capturecollection?view=netframework-4.8#methods
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.group?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.groupcollection?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.matchcollection?view=netframework-4.8

©sideway
close

References

  1. Active Server Pages, http://msdn.microsoft.com/en-us/library/aa286483.aspx, updated 20Dec2012
  2. ASP Overview, http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx, updated 21Dec2012
  3. ASP Best Practices, http://technet.microsoft.com/en-us/library/cc939157.aspx, updated 15Apr2013
  4. ASP Built-in Objects, http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx, updated 20Dec2012
  5. Response Object, http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx, updated 20Dec2012
  6. Request Object, http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx, updated 20Dec2012
  7. Server Object (IIS), http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx, updated 20Dec2012
  8. Application Object (IIS), http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx, updated 20Dec2012
  9. Session Object (IIS), http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx, updated 20Dec2012
  10. ASPError Object, http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx, updated 20Dec2012
  11. ObjectContext Object (IIS), http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx, updated 20Dec2012
  12. Debugging Global.asa Files, http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx, updated 15Apr2013
  13. How to: Debug Global.asa files, http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx, updated 15Apr2013
  14. Calling COM Components from ASP Pages, http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx, updated 15Apr2013
  15. IIS ASP Scripting Reference, http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx, updated 15Apr2013
  16. ASP Keywords, http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx, updated 15Apr2013
  17. Creating Simple ASP Pages, http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx, updated 15Apr2013
  18. Including Files in ASP Applications, http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx, updated 15Apr2013
  19. ASP Overview, http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx, updated 15Apr2013
  20. FileSystemObject Object, http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx, updated 14Mayr2013
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx, ADO Object Model, updated 15Apr2013
  22. ADO Fundamentals, http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx, updated 14Mayr2013
close

ID: 190700022 Last Updated: 2019/7/22 Revision: Ref:

IMAGE

Home (5)

Business

Management

HBR (3)

Information

Recreation

Hobbies (7)

Culture

Chinese (1097)

English (336)

Reference (66)

Computer

Hardware (149)

Software

Application (187)

Digitization (24)

Numeric (19)

Programming

Web (648)new

CSS (SC)

ASP.NET (SC)

Regular Expression (SC)

HTML

Knowledge Base

Common Color (SC)

Html Entity (Unicode) (SC)

Html 401 Special (SC)

OS (389)

MS Windows

Windows10 (SC)

.NET Framework (SC)

DeskTop (7)

Knowledge

Mathematics

Formulas (8)

Number Theory (206)

Algebra (20)

Trigonometry (18)

Geometry (18)

Calculus (67)

Complex Analysis (21)

Engineering

Tables (8)

Mechanical

Mechanics (1)

Rigid Bodies

Statics (92)

Dynamics (37)

Fluid (5)

Fluid Kinematics (5)

Control

Process Control (1)

Acoustics (19)

FiniteElement (2)

Biology (1)

Geography (1)


Copyright © 2000-2019 Sideway . All rights reserved Disclaimers last modified on 10 Feb 2019