@@ -58,8 +58,8 @@ extension RegexComponent {
58
58
/// Returns a regular expression where the start and end of input
59
59
/// anchors (`^` and `$`) also match against the start and end of a line.
60
60
///
61
- /// This method corresponds to applying the `m` option in a regular
62
- /// expression literal. For this behavior in the `RegexBuilder` syntax, see
61
+ /// This method corresponds to applying the `m` option in regex syntax. For
62
+ /// this behavior in the `RegexBuilder` syntax, see
63
63
/// ``Anchor.startOfLine``, ``Anchor.endOfLine``, ``Anchor.startOfInput``,
64
64
/// and ``Anchor.endOfInput``.
65
65
///
@@ -69,16 +69,22 @@ extension RegexComponent {
69
69
wrapInOption ( . multiline, addingIf: matchLineEndings)
70
70
}
71
71
72
- /// Returns a regular expression where quantifiers are reluctant by default
73
- /// instead of eager .
72
+ /// Returns a regular expression where quantifiers use the specified behavior
73
+ /// by default .
74
74
///
75
- /// This method corresponds to applying the `U` option in a regular
76
- /// expression literal .
75
+ /// This setting does not affect calls to quantifier methods, such as
76
+ /// `OneOrMore`, that include an explicit `behavior` parameter .
77
77
///
78
- /// - Parameter useReluctantQuantifiers: A Boolean value indicating whether
79
- /// quantifiers should be reluctant by default.
80
- public func reluctantQuantifiers( _ useReluctantQuantifiers: Bool = true ) -> Regex < RegexOutput > {
81
- wrapInOption ( . reluctantByDefault, addingIf: useReluctantQuantifiers)
78
+ /// Passing `.eager` or `.reluctant` to this method corresponds to applying
79
+ /// the `(?-U)` or `(?U)` option in regex syntax, respectively.
80
+ ///
81
+ /// - Parameter behavior: The default behavior to use for quantifiers.
82
+ public func repetitionBehavior( _ behavior: RegexRepetitionBehavior ) -> Regex < RegexOutput > {
83
+ if behavior == . possessive {
84
+ return wrapInOption ( . possessiveByDefault, addingIf: true )
85
+ } else {
86
+ return wrapInOption ( . reluctantByDefault, addingIf: behavior == . reluctant)
87
+ }
82
88
}
83
89
84
90
/// Returns a regular expression that matches with the specified semantic
@@ -183,6 +189,46 @@ public struct RegexWordBoundaryKind: Hashable {
183
189
}
184
190
}
185
191
192
+ /// Specifies how much to attempt to match when using a quantifier.
193
+ @available ( SwiftStdlib 5 . 7 , * )
194
+ public struct RegexRepetitionBehavior : Hashable {
195
+ internal enum Kind {
196
+ case eager
197
+ case reluctant
198
+ case possessive
199
+ }
200
+
201
+ var kind : Kind
202
+
203
+ @_spi ( RegexBuilder) public var dslTreeKind : DSLTree . _AST . QuantificationKind {
204
+ switch kind {
205
+ case . eager: return . eager
206
+ case . reluctant: return . reluctant
207
+ case . possessive: return . possessive
208
+ }
209
+ }
210
+ }
211
+
212
+ @available ( SwiftStdlib 5 . 7 , * )
213
+ extension RegexRepetitionBehavior {
214
+ /// Match as much of the input string as possible, backtracking when
215
+ /// necessary.
216
+ public static var eager : Self {
217
+ . init( kind: . eager)
218
+ }
219
+
220
+ /// Match as little of the input string as possible, expanding the matched
221
+ /// region as necessary to complete a match.
222
+ public static var reluctant : Self {
223
+ . init( kind: . reluctant)
224
+ }
225
+
226
+ /// Match as much of the input string as possible, performing no backtracking.
227
+ public static var possessive : Self {
228
+ . init( kind: . possessive)
229
+ }
230
+ }
231
+
186
232
// MARK: - Helper method
187
233
188
234
@available ( SwiftStdlib 5 . 7 , * )
0 commit comments