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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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.
=~
operator supportswitch
operator) supportPath 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.
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.
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
.
Add the following to your Cartfile:
ub "crossroadlabs/Regex"
Run carthage update
and follow the steps as described in Carthage's README.
`/Regex
` folder in your project.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
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
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
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
replaced = "(.+?)([123]*)(.*)".r?.replaceAll(in: "l321321alala", with: "$1-$2-$3")
placed is "l-321321-alala"
replaced = "(.+?)([123]+)(.+?)".r?.replaceAll(in: "l321321la321a") { match in
if match.group(at: 1) == "l" {
return nil
} else {
return match.matched.uppercaseString
}
placed is "l321321lA321A"
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"]
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", "."]
You can view the CHANGELOG as a separate document here.
To get started, sign the Contributor License Agreement.