You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`tryCapture(_:)` works like `capture(_:)` but accepts an optional or throwing capture transform instead. When the transform fails (returns `nil` or throws), it will cause matching to fail. This works well with failable conversion initializers such as `Int.init?(_:)`, `Unicode.Scalar.init?(_:)`, etc.
----
Example:
With `tryCapture`, the following example avoids unnecessary levels of optionals and double optionals.
```swift
line.match {
OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:))
Optionally {
".."
OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:))
}
OneOrMore(.whitespace)
";"
OneOrMore(.whitespace)
OneOrMore(.word).tryCapture(Unicode.GraphemeBreakProperty.init)
Repeat(.any)
} // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Unicode.GraphemeBreakProperty)>
```
0 commit comments