wireapp/Regex

Name: Regex

Owner: Wire Swiss GmbH

Description: Regular expressions for swift

Forked from: crossroadlabs/Regex

Created: 2016-09-12 13:42:13.0

Updated: 2017-10-07 16:31:52.0

Pushed: 2018-04-12 13:46:31.0

Homepage: null

Size: 353

Language: Swift

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

by Crossroad Labs

Regex

? linux: ready GitHub license Build Status GitHub release Carthage compatible CocoaPods version Platform OS X | iOS | tvOS | watchOS | Linux

Advanced regular expressions for Swift
Goals

Regex library was mainly introduced to fulfill the needs of Swift Express - web application server side framework for Swift.

Still we hope it will be useful for everybody else.

Happy regexing ;)

Features
Extra

Path to Regex converter is available as a separate library here: PathToRegex

This one allows using path patterns like /folder/*/:file.txt or /route/:one/:two to be converted to Regular Expressions and matched against strings.

Getting started
Installation
Package Manager

Add the following dependency to your Package.swift:

kage(url: "https://github.com/crossroadlabs/Regex.git", majorVersion: 0)

Run `swift build` and build your app. Package manager is supported on OS X, but it's still recommended to be used on Linux only.

CocoaPods

Add the following to your Podfile:

'CrossroadRegex'

Make sure that you are integrating your dependencies using frameworks: add use_frameworks! to your Podfile. Then run pod install.

Carthage

Add the following to your Cartfile:

ub "crossroadlabs/Regex"

Run carthage update and follow the steps as described in Carthage's README.

Manually
  1. Download and drop `/Regex` folder in your project.
  2. Congratulations!
Examples
Hello Regex:

All the lines below are identical and represent simple matching. All operators and matches function return Bool

erator way, can match either regex or string containing pattern
1321alala" =~ "(.+?)([123]*)(.*)".r
1321alala" =~ "(.+?)([123]*)(.*)"

milar function
?)([123]*)(.*)".r!.matches("l321321alala")

Operator !~ returns true if expression does NOT match:

1321alala" !~ "(.+?)([123]*)(.*)".r
1321alala" !~ "(.+?)([123]*)(.*)"
th return false
Swift Pattern Matching (aka switch keyword)

Regex provides very deep integration with Swift and can be used with the switch keyword in the following way:

letter = "a"
digit = "1"
other = "!"

u just put your string is a regular Swift's switch to match to regular expressions
ch letter {
//note .r after the string literal of the pattern
case "\\d".r: print("digit")
case "[a-z]".r: print("letter")
default: print("bizarre symbol")


ch digit {
case "\\d".r: print("digit")
case "[a-z]".r: print("letter")
default: print("bizarre symbol")


ch other {
//note .r after the string literal of the pattern
case "\\d".r: print("digit")
case "[a-z]".r: print("letter")
default: print("bizarre symbol")

The output of the code above will be:

er
t
rre symbol
Accessing groups:
trings can be converted to regex in Scala style .r property of a string
digits = "(.+?)([123]*)(.*)".r?.findFirst(in: "l321321alala")?.group(at: 2)
igits is "321321" here
Named groups:
regex:RegexType = try Regex(pattern:"(.+?)([123]*)(.*)",
                                    groupNames:"letter", "digits", "rest")
match = regex.findFirst(in: "l321321alala")
et match = match {
let letter = match.group(named: "letter")
let digits = match.group(named: "digits")
let rest = match.group(named: "rest")
//do something with extracted data

Replace:
replaced = "(.+?)([123]*)(.*)".r?.replaceAll(in: "l321321alala", with: "$1-$2-$3")
placed is "l-321321-alala"
Replace with custom replacer function:
replaced = "(.+?)([123]+)(.+?)".r?.replaceAll(in: "l321321la321a") { match in
if match.group(at: 1) == "l" {
    return nil
} else {
    return match.matched.uppercaseString
}

placed is "l321321lA321A"
Split:

In the following example, split() looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces from the string. nameList is the array returned as a result of split().

names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand"
nameList = names.split(using: "\\s*;\\s*".r)
me list contains ["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand"]
Split with groups:

If separator contains capturing parentheses, matched results are returned in the array.

myString = "Hello 1 word. Sentence number 2."
splits = myString.split(using: "(\\d)".r)
lits contains ["Hello ", "1", " word. Sentence number ", "2", "."]
Roadmap
Changelog

You can view the CHANGELOG as a separate document here.

Contributing

To get started, sign the Contributor License Agreement.

Crossroad Labs by Crossroad Labs

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.