SwiftRegex is a wrapper library of regular expression libraries, such as POSIX regex.h.
To use swift-regex...
- Specify swift-regex implementation, using
SwiftRegexImplementation.by
object. - An implementation will offer its DSL to configure.
import POSIXRegex
import SwiftRegex
SwiftRegexImplementation.by.usePosix()
guard let regex = pattern(of: "ba").compile() else {
fatalError { "cannot compile pattern \"ba\"" }
}
let matcher = regex.matcher(for: "foo-bar-baz")
matcher.matches // -> true
Currently POSIX regex.h
is the only implementation of SwiftRegex.
- Add API and POSIX implementation of a feature to replace string element.
- Add implementation of RE2
- Add implementation of Oniguruma
SwiftRegex uses Swift Package Manager, so you can install this library by SPM.
First, add SwiftRegex to dependencies
.
dependencies [
.package(url: "https://github.com/mike-neck/swift-regex.git", from: "0.1"),
]
Second, add SwiftRegex
and an implementation to your target dependencies.
targets [
.target(name: "YourAweSomeApp", dependencies: [
"SwiftRegex",
"POSIXRegex",
]),
]
Third, import SwiftRegex
(which determines API) and an implementation in your swift file.
import SwiftRegex // defines API
import POSIXRegex // offers implementation
SwiftRegexImplementation.by.usePosix() // by calling this, pattern function will returns POSIX regexp.
To implement SwiftRegex, you should offer some protocols.
SwiftRegexImplementationRef
- The function
createPattern
returnsSwiftRegexPattern
protocol. - This protocol is only for a smooth DSL.
- The function
SwiftRegexPattern
- The function
compile
returnsSwiftRegex?
- This protocol is responsible for compiling a given regex pattern.
- The function
SwiftRegex
- The function
matcher
returnsRegexMatcher
. - Represents a compiled regular expression.
- The function
RegexMatcher
- Represents matching result.
- offering some matching result.
matches
returns whether the pattern matches the testing string.
- And extend
SwiftRegexImplementation
class to offer DSL, and makepattern
function to return your implementation.- A static property
by
is a singleton object ofSwiftRegexImplementation
. - Calling
useImpl(of:)
withSwiftRegexImplementationRef
from your extension function forSwiftRegexImplementation
will makes it available forpattern
function to return your SwiftRegex implementation.
- A static property