Git Product home page Git Product logo

regex's Introduction

Regex.swift

Build Status CocoaPods CocoaPods CocoaPods GitHub tag

install

Use CocoaPods.

Add to your Podfile:

pod 'Regex'

And then run pod install from the shell:

$ 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 Strings much as one would expect from its Javascript equivalent.

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.

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().

"Winnie the Pooh" =~ Regex("\\s+(the)\\s+")  // returns a Regex.MatchResult

Quickly loop over a Regex's captures:

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().

func map (regexResult:Regex.MatchResult, replacementTemplate:String) -> String

You can use it like so:

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: https://github.com/brynbellomy/Funky), it's a little less wordy:

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

regex's People

Contributors

brynbellomy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

regex's Issues

Manual Installation

CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

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.