Regular Expression
Quantifiers Greedy and Lazy Quantifiers .NET Quantifiers Match Zero or More Times: * Match One or More Times: + Match Zero or One Time: ? Match Exactly n Times: {n} Match at Least n Times: {n,} Match Between n and m Times: {n,m} Match Zero or More Times (Lazy Match): *? Match One or More Times (Lazy Match): +? Match Zero or One Time (Lazy Match): ?? Match Exactly n Times (Lazy Match): {n}? Match at Least n Times (Lazy Match): {n,}? Match Between n and m Times (Lazy Match): {n,m}? QuantQuantifiers and Empty Matches Nesting Quantifier Examples See also Source/Referencee
Regular Expression
Quantifiers
A quantifier is used to specify how many instances of the preceding pattern, e.g. a character, group, or character class, must be present in the input for a match to be found..
a symbol is used to represent a quantifier. e.g. *, +, and ?.
a pair of Curly brackets is used to specify a quantifier, e.g. {n}, {n,}, and {n,m}.
If the *, +, ?, {, and } characters are encountered in a regular expression pattern, the regular expression engine interprets them as quantifiers or part of quantifier constructs unless they are included in a character class. To interpret these as literal characters outside a character class, they must be escaped by preceding them with a backslash.
Greedy and Lazy Quantifiers
There are two versions quantifiers, a greedy version and a non-greedy (or lazy) version.
A greedy quantifier tries to match an element as many times as possible. e.g. *, +, ?, {n}, {n,}, and {n,m}.
A non-greedy or lazy quantifier tries to match an element as few times as possible. A greedy quantifier can be turned into a lazy quantifier by simply adding a ? after the greedy quantifier. e.g. *?, +?, ??, {n}?, {n,}?, and {n,m}?.
In other words, a lazy quantifier always tries to complete the match as few as possible.
.NET Quantifiers
Match Zero or More Times: *
The * quantifier matches the preceding element zero or more times. It is equivalent to the {0,} quantifier. * is a greedy quantifier whose lazy equivalent is *?.
Match One or More Times: +
The + quantifier matches the preceding element one or more times. It is equivalent to {1,}. + is a greedy quantifier whose lazy equivalent is +?.
Match Zero or One Time: ?
The ? quantifier matches the preceding element zero or one time. It is equivalent to {0,1}. ? is a greedy quantifier whose lazy equivalent is ??.
Match Exactly n Times: {n}
The {n} quantifier matches the preceding element exactly n times, where n is any integer. {n} is a greedy quantifier whose lazy equivalent is {n}?.
Match at Least n Times: {n,}
The {n,} quantifier matches the preceding element at least n times, where n is any integer. {n,} is a greedy quantifier whose lazy equivalent is {n,}?.
Match Between n and m Times: {n,m}
The {n,m} quantifier matches the preceding element at least n times, but no more than m times, where n and m are integers. {n,m} is a greedy quantifier whose lazy equivalent is {n,m}?.
Match Zero or More Times (Lazy Match): *?
The *? quantifier matches the preceding element zero or more times, but as few times as possible. It is the lazy counterpart of the greedy quantifier *.
Match One or More Times (Lazy Match): +?
The +? quantifier matches the preceding element one or more times, but as few times as possible. It is the lazy counterpart of the greedy quantifier +.
Match Zero or One Time (Lazy Match): ??
The ?? quantifier matches the preceding element zero or one time, but as few times as possible. It is the lazy counterpart of the greedy quantifier ?.
Match Exactly n Times (Lazy Match): {n}?
The {n}? quantifier matches the preceding element exactly n times, where n is any integer. It is the lazy counterpart of the greedy quantifier {n}+.
Match at Least n Times (Lazy Match): {n,}?
The {n,}? quantifier matches the preceding element at least n times, where n is any integer, but as few times as possible. It is the lazy counterpart of the greedy quantifier {n,}.
Match Between n and m Times (Lazy Match): {n,m}?
The {n,m}? quantifier matches the preceding element between n and m times, where n and m are integers, but as few times as possible. It is the lazy counterpart of the greedy quantifier {n,m}.
QuantQuantifiers and Empty Matches
The quantifiers *, +, and {n,m} and their lazy counterparts never repeat after an empty match when the minimum number of captures has been found. This rule prevents quantifiers from entering infinite loops on empty subexpression matches when the maximum number of possible group captures is infinite or near infinite.
Nesting Quantifier
Nesting quantifiers (for example, as the regular expression pattern (a*)* does) can increase the number of comparisons that the regular expression engine must perform, as an exponential function of the number of characters in the input string. For more information about this behavior and its workarounds, see Backtracking.
Examples
Examples of Quantifiers
ASP.NET Code Input:
<!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 xstring As String = "01234 67 901"
Dim xmatchstr As String = ""
xmatchstr = xmatchstr & "Given string: """ & xstring & """<br />"
xmatchstr = xmatchstr & showresult(xstring,"\w *")
xmatchstr = xmatchstr & showresult(xstring,"(\w *)*")
xmatchstr = xmatchstr & showresult(xstring,"\w+ +")
xmatchstr = xmatchstr & showresult(xstring,"(\w+ +)+")
xmatchstr = xmatchstr & showresult(xstring,"\w? ?")
xmatchstr = xmatchstr & showresult(xstring,"(\w? ?)?")
xmatchstr = xmatchstr & showresult(xstring,"\w* *")
xmatchstr = xmatchstr & showresult(xstring,"(\w* *)*")
xmatchstr = xmatchstr & showresult(xstring,"(\w* *)*?")
xmatchstr = xmatchstr & showresult(xstring,"(\w* *)+")
xmatchstr = xmatchstr & showresult(xstring,"(\w* *)+?")
xmatchstr = xmatchstr & showresult(xstring,"(\w* *)?")
xmatchstr = xmatchstr & showresult(xstring,"(\w* *)??")
xmatchstr = xmatchstr & showresult(xstring,"(\w){2}")
xmatchstr = xmatchstr & showresult(xstring,"(\w){2}?")
xmatchstr = xmatchstr & showresult(xstring,"(\w){1,2}")
xmatchstr = xmatchstr & showresult(xstring,"(\w){1,2}?")
xmatchstr = xmatchstr & showresult(xstring,"(\w){2,}")
xmatchstr = xmatchstr & showresult(xstring,"(\w){2,}?")
lbl01.Text = xmatchstr
End Sub
Function showresult(xstring,xpattern)
Dim xmatch As Match
Dim xcaptures As CaptureCollection
Dim ycaptures As CaptureCollection
Dim xgroups As GroupCollection
Dim xmatchstr As String = ""
Dim xint As Integer
Dim yint As Integer
Dim zint As Integer
xmatchstr = xmatchstr & "<br />Result of Regex.Match(string,""" & xpattern & """): """
xmatch = Regex.Match(xstring,xpattern)
xmatchstr = xmatchstr & xmatch.value & """<br />"
xcaptures = xmatch.Captures
xmatchstr = xmatchstr & "->Result of CaptureCollection.Count: """
xmatchstr = xmatchstr & xcaptures.Count & """<br />"
For xint = 0 to xcaptures.Count - 1
xmatchstr = xmatchstr & "->->Result of CaptureCollection("& xint & ").Value, Index, Length: """
xmatchstr = xmatchstr & xcaptures(xint).Value & ", " & xcaptures(xint).Index & ", " & xcaptures(xint).Length & """<br />"
xgroups = xmatch.Groups
xmatchstr = xmatchstr & "->Result of GroupCollection.Count: """
xmatchstr = xmatchstr & xgroups.Count & """<br />"
For yint = 0 to xgroups.Count - 1
xmatchstr = xmatchstr & "->->Result of GroupCollection("& yint & ").Value, Index, Length: """
xmatchstr = xmatchstr & xgroups(yint).Value & ", " & xgroups(yint).Index & ", " & xgroups(yint).Length & """<br />"
ycaptures = xgroups(yint).Captures
xmatchstr = xmatchstr & "->->->Result of CaptureCollection.Count: """
xmatchstr = xmatchstr & ycaptures.Count & """<br />"
For zint = 0 to ycaptures.Count - 1
xmatchstr = xmatchstr & "->->->->Result of CaptureCollection("& zint & ").Value, Index, Length: """
xmatchstr = xmatchstr & ycaptures(zint).Value & ", " & ycaptures(zint).Index & ", " & ycaptures(zint).Length & """<br />"
Next
Next
Next
Return xmatchstr
End Function
</script>
</head>
<body>
<% Response.Write ("<h1>This is a Sample Page of Quantifiers</h1>") %>
<p>
<%-- Set on Page_Load --%>
<asp:Label id="lbl01" runat="server" />
</p>
</body>
</html>