If you can come up with a good name I'm all s.
matches(_:)
comes to mind, but that's already used to get the collection of all non-overlapping matches. Another could be isMatched(by:)
.
The ~=
is a little hard to discover, but it does work fairly well after becoming familiar with it:
let x = myRegex ~= string ? foo() : bar()
And this is equivalent to
let x = string.wholeMatch(of: myRegex) != nil ? foo() : bar()
You'd be able to interpolate any RegexComponent
, which would bring it to parity with the builder DSL in that respect. Beyond the convenience of not having to split the literal, it doesn't add too much more composition capabilities. It might allow you interpolate in between a capture and a backreference, but I suspect that isn't a super-common need (there's an argument to be made that the parser should reject certain forms of that anyways if an interpolation changes e.g. relative capture numbering).