Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fpg1503/regex.swift
๐ A Simple Swift NSRegularExpression wrapper
https://github.com/fpg1503/regex.swift
cocoapods crash-safe ios lightweight macos microframework production-ready regex swift tvos watchos
Last synced: 26 days ago
JSON representation
๐ A Simple Swift NSRegularExpression wrapper
- Host: GitHub
- URL: https://github.com/fpg1503/regex.swift
- Owner: fpg1503
- License: mit
- Created: 2016-06-05T12:25:15.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-06-26T15:10:43.000Z (over 3 years ago)
- Last Synced: 2024-03-15T00:22:15.707Z (9 months ago)
- Topics: cocoapods, crash-safe, ios, lightweight, macos, microframework, production-ready, regex, swift, tvos, watchos
- Language: Swift
- Homepage:
- Size: 47.9 KB
- Stars: 9
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Regex.swift
[![CI Status](http://img.shields.io/travis/fpg1503/Regex.swift.svg?style=flat)](https://travis-ci.org/fpg1503/Regex.swift)
[![Version](https://img.shields.io/cocoapods/v/Regex.swift.svg?style=flat)](http://cocoapods.org/pods/Regex.swift)
[![License](https://img.shields.io/cocoapods/l/Regex.swift.svg?style=flat)](http://cocoapods.org/pods/Regex.swift)
[![Platform](https://img.shields.io/cocoapods/p/Regex.swift.svg?style=flat)](http://cocoapods.org/pods/Regex.swift)`Regex.swift` is yet another Regex ฮผframework. The main difference is that it's only 68 lines long and intends to keep things simple yet readable. It's been used in production by millions of users and so far has proved itself 100% crash-free.
There are only 2 types: [`Match`](https://github.com/fpg1503/Regex.swift/blob/master/Source/Regex.swift#L28) and [`Regex`](https://github.com/fpg1503/Regex.swift/blob/master/Source/Regex.swift#L40). If I were you I'd read the whole file, it's really quick and will give you a better understanding.
For Swift 2.2 use version 0.1.1.
For Swift 3.x use version 1.0.3
For Swift 4.0+ use version 2.0+## Usage
Simply create a `Regex` by using `Regex(pattern:)` or `Regex(pattern: options)`.
To verify if a `String` matches a `Regex` simply use `regex.match(string:)`:
```swift
func validatePlate(plate: String) -> Bool {
guard let regex = Regex(pattern: "^\\w{3}-?\\d{4}$") else { return false }return regex.match(plate)
}
```If you want to grab the mathes use [`regex.matches(string:)`](https://github.com/fpg1503/Regex.swift/blob/master/Regex.swift/Classes/Regex.swift#L62), it returns an array of [`Match`](https://github.com/fpg1503/Regex.swift/blob/master/Regex.swift/Classes/Regex.swift#L28):
```swift
public init?(plate: String) {
guard let regex = Regex(pattern: "^(\\w{3})-?(\\d{4})$"),
captureGroups = regex.matches(plate).first?.captureGroups
where captureGroups.count == 2 else { return nil }letters = captureGroups[0]
numbers = captureGroups[1]
}
```## Requirements
- `Regex.swift` relies on `NSRegularExpression` and hopefully won't be necessary anymore when Swift gets a *Swifty* standard Regex API.- As of version 1.0 `Regex.swift` uses Swift 3.0.
## Installation
[`Regex.swift`](https://github.com/fpg1503/Regex.swift/blob/master/Regex.swift/Classes/Regex.swift) is a single file with 68 lines. You can simply download it and add to your project or use CocoaPods.
Regex.swift is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod "Regex.swift"
```## Contributing
PRs and issues welcome. The only rule here is: keep it as simple as possible.## FAQ
Why doesn't `Regex` conform to `StringLiteralConvertible`?
- Because I want its initializer to be failable.## Author
Francesco Perrotti-Garcia, [email protected]
## License
Regex.swift is available under the MIT license. See the LICENSE file for more info.