Skip to content

Commit b68a030

Browse files
authored
Merge pull request #212 from rxwei/dsl-pitch-matchresult
Add `Regex.Match.subscript(_: Reference)` to DSL pitch.
2 parents 0f9f0a4 + 0db0ce6 commit b68a030

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Documentation/Evolution/RegexBuilderDSL.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,21 +1195,24 @@ public struct Reference<Capture>: RegexComponent {
11951195
public init(_ captureType: Capture.Type = Capture.self)
11961196
public var regex: Regex<Capture>
11971197
}
1198+
1199+
extension Regex.Match {
1200+
public subscript<Capture>(_ reference: Reference<Capture>) -> Capture { get }
1201+
}
11981202
```
11991203

1200-
When capturing some regex with a reference specified, the reference will refer to the most recently captured content. The reference itself can be used as a regex to match the most recently captured content. This API is also designed to work with match result lookup, which will be introduced in a different pitch.
1204+
When capturing some regex with a reference specified, the reference will refer to the most recently captured content. The reference itself can be used as a regex to match the most recently captured content, or as a name to look up the result of matching.
12011205

12021206
```swift
1203-
let a = Reference()
1204-
let b = Reference()
1207+
let a = Reference(Substring.self)
1208+
let b = Reference(Substring.self)
12051209
let regex = Regex {
12061210
Capture("abc", as: a)
12071211
Capture("def", as: b)
12081212
a
12091213
Capture(b)
12101214
}
12111215

1212-
// To be introduced in a different pitch:
12131216
if let result = input.firstMatch(of: regex) {
12141217
print(result[a]) // => "abc"
12151218
print(result[b]) // => "def"

0 commit comments

Comments
 (0)