Coder Social home page Coder Social logo

regex's Introduction

by Crossroad Labs

Regex

๐Ÿง linux: ready ![GitHub license](https://img.shields.io/badge/license-Apache 2.0-lightgrey.svg) 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 ;)

Getting started

Installation

Package Manager

Add the following dependency to your Package.swift:

.Package(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:

pod '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:

github "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

//operator way, can match either regex or string containing pattern
"l321321alala" =~ "(.+?)([1,2,3]*)(.*)".r
"l321321alala" =~ "(.+?)([1,2,3]*)(.*)"

//similar function
"(.+?)([1,2,3]*)(.*)".r!.matches("l321321alala")

Operator !~ returns true if expression does NOT match:

"l321321alala" !~ "(.+?)([1,2,3]*)(.*)".r
"l321321alala" !~ "(.+?)([1,2,3]*)(.*)"
//both return false

Accessing groups:

// strings can be converted to regex in Scala style .r property of a string
let digits = "(.+?)([1,2,3]*)(.*)".r?.findFirst(in: "l321321alala")?.group(at: 2)
// digits is "321321" here

Named groups:

let regex:RegexType = try Regex(pattern:"(.+?)([1,2,3]*)(.*)",
                                        groupNames:"letter", "digits", "rest")
let match = regex.findFirst(in: "l321321alala")
if let 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:

let replaced = "(.+?)([1,2,3]*)(.*)".r?.replaceAll(in: "l321321alala", with: "$1-$2-$3")
//replaced is "l-321321-alala"

Replace with custom replacer function:

let replaced = "(.+?)([1,2,3]+)(.+?)".r?.replaceAll(in: "l321321la321a") { match in
	if match.group(at: 1) == "l" {
		return nil
	} else {
		return match.matched.uppercaseString
	}
}
//replaced 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().

let names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand"
let nameList = names.split(using: "\\s*;\\s*".r)
//name 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.

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

Roadmap

  • v1.0: stable release (once we will see that no issues are coming)

Changelog

  • v0.7
    • Support of Swift 3.0 preview 1
    • Regex options support (like case sensetivity)
    • Breaking change (Swift 3.0ish syntax)
    • Renamed module in Pod from CrossroadRegex to Regex
  • v0.6
    • Swift 3.0 support
  • v0.5.1
    • Minor linux build related fixes
  • v0.5
    • package manager support
    • full linux support ๐Ÿง
  • v0.4.1
    • support for optionally present groups
  • v0.4
    • iOS, tvOS and watchOS support
    • Pod supports watchOS
    • automated pod deployment
  • v0.3
    • Split
    • Matches
    • CocoaPod
    • Syntactic sugar operators (=~ and !~)
  • v0.2
    • Replace functions
    • Carthage support
  • v0.1
    • basic find functions for OS X and iOS

Contributing

To get started, sign the Contributor License Agreement.

Crossroad Labs by Crossroad Labs

regex's People

Contributors

dileping avatar lfarah avatar nayzak avatar ypopovych avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.