Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brynbellomy/Regex
Regex class for Swift. Wraps NSRegularExpression.
https://github.com/brynbellomy/Regex
Last synced: 6 days ago
JSON representation
Regex class for Swift. Wraps NSRegularExpression.
- Host: GitHub
- URL: https://github.com/brynbellomy/Regex
- Owner: brynbellomy
- License: mit
- Created: 2015-02-11T00:29:46.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-07-04T19:08:32.000Z (over 7 years ago)
- Last Synced: 2024-11-29T18:49:51.704Z (13 days ago)
- Language: Swift
- Homepage: http://brynbellomy.github.io/Regex/
- Size: 275 KB
- Stars: 67
- Watchers: 5
- Forks: 16
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-ios - Regex - Regex class for Swift. Wraps NSRegularExpression. (Regex / React-Like)
- awesome-ios-star - Regex - Regex class for Swift. Wraps NSRegularExpression. (Regex / React-Like)
- awesome-swift-cn - Regex by brynbellomy - Swift wrapper class for `NSRegularExpression`. Includes a `map()` override and an operator (`=~`) for a more Swift-like API. (Libs / Text)
README
# Regex.swift
[![Build Status](https://travis-ci.org/brynbellomy/Regex.svg?branch=0.2.1)](https://travis-ci.org/brynbellomy/Regex)
[![CocoaPods](https://img.shields.io/cocoapods/v/Regex.svg?style=flat)](http://cocoadocs.org/docsets/Regex)
[![CocoaPods](https://img.shields.io/cocoapods/p/Regex.svg?style=flat)](http://cocoadocs.org/docsets/Regex)
[![CocoaPods](https://img.shields.io/cocoapods/l/Regex.svg?style=flat)](http://cocoadocs.org/docsets/Regex)
[![GitHub tag](https://img.shields.io/github/tag/brynbellomy/Regex.svg?style=flat)]()# install
Use [CocoaPods](https://cocoapods.org/).
Add to your `Podfile`:
```ruby
pod 'Regex'
```And then run `pod install` from the shell:
```sh
$ pod install
```# usage
### Simple use cases: `String` extension methods
**String.grep()**
This method is modeled after Javascript's `String.match()`. It returns a `Regex.MatchResult` object. The object's `captures` property is an array of `String`s much as one would expect from its Javascript equivalent.
```swift
let result = "Winnie the Pooh".grep("\\s+([a-z]+)\\s+")result.searchString == "Winnie the Pooh"
result.captures.count == 2
result.captures[0] == " the "
result.captures[1] == "the"
result.boolValue == true // `boolValue` is `true` if there were more than 0 matches// You can use `grep()` in conditionals because of the `boolValue` property its result exposes
let emailAddress = "bryn&typos.org"
if !emailAddress.grep("@") {
// that's not an email address!
}
```**String.replaceRegex()**
This method is modeled after the version of Javascript's `String.replace()` that accepts a Regex parameter.
```swift
let name = "Winnie the Pooh"
let darkName = name.replaceRegex("Winnie the ([a-zA-Z]+)", with: "Darth $1")
// darkName == "Darth Pooh"
```### Advanced use cases: `Regex` object and operators
**operator =~**
You can use the `=~` operator to search a `String` (the left operand) for a `Regex` (the right operand). It's the same as calling `theString.grep("the regex pattern")`, but might be more clear in some cases. It returns the same `Regex.MatchResult` object as `String.grep()`.
```swift
"Winnie the Pooh" =~ Regex("\\s+(the)\\s+") // returns a Regex.MatchResult
```Quickly loop over a `Regex`'s captures:
```swift
for capture in ("Winnie the Pooh" =~ Regex("\\s+(the)\\s+")).captures {
// capture is a String
}
```# Overriden `map()` function for substitution
A more "functional programming" way of doing string replacement is possible via an override for `map()`. In keeping with the overall aim to avoid reinventing a perfectly good wheel (i.e., `NSRegularExpression`), this function simply calls through to `NSRegularExpression.replaceMatchesInString()`.
```swift
func map (regexResult:Regex.MatchResult, replacementTemplate:String) -> String
```You can use it like so:
```swift
let stageName = map("Winnie the Pooh" =~ Regex("([a-zA-Z]+)\\s+(the)(.*)"), "$2 $1")
// stageName == "the Winnie"
```Or if you have some functional operators lying around (for example: ), it's a little less wordy:
```swift
("Winnie the Pooh" =~ Regex("([a-zA-Z]+)\\s+(the)(.*)")) |> map‡("$2 $1")
```... but you have to be as crazy as me to find that more readable than `"Winnie".replaceRegex(_:withString:)`, so no pressure.
# contributors / authors
- bryn austin bellomy ()