PySwiftyRegex - Easily deal with Regex in Swift in a Pythonic way

Overview

PySwiftyRegex

Cocoapods Compatible License Carthage Compatible Platform Twitter

Easily deal with Regex in Swift in a Pythonic way.

简体中文 日本語 한국어

This is Easy

import PySwiftyRegex

if let m = re.search("[Tt]his is (.*?)easy", "I think this is really easy!!!") {
	m.group()  // "this is really easy"
	m.group(1) // "really "
}

See More examples.

Requirements

  • iOS 7.0+ / Mac OS X 10.9+
  • Xcode 8.0+

< For Swift 2.3 please use version 0.3.0.

Installation

Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.

To use PySwiftyRegex with a project targeting iOS 7, consider using CocoaSeeds or copy the PySwiftyRegex.swift file into your project.

CocoaPods(iOS 8+, OS X 10.9+)

You can use Cocoapods to install PySwiftyRegex by adding it to your to your Podfile:

platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
	pod 'PySwiftyRegex', '~> 1.0.0'
end

Then, run the following command:

$ pod install

Carthage(iOS 8+, OS X 10.9+)

Adding the following line to your Cartfile or Cartfile.private:

1.0.0 ">
github "cezheng/PySwiftyRegex" ~> 1.0.0

Run the following command:

$ carthage update

Then drag the PySwiftyRegex.framework built by Carthage into your target's General -> Embedded Binaries.

CocoaSeeds (for iOS 7)

CocoaSeeds allows you to use Swift libraries in iOS 7 projects.

Create Seedfile:

target :MyApp do
  github 'cezheng/PySwiftyRegex', '1.0.0', :files => 'PySwiftyRegex/PySwiftyRegex.swift'
end

Then run the following command:

$ seed install

Now you can see the PySwiftyRegex.swift file in your Xcode project. Build and enjoy!

Supported re methods

If you are familiar with Python's re module, you are ready to go. If not, you may like to check how Python's re is better than the cumbersome NSRegularExpression's APIs, by clicking at the items below.

re

re.RegexObject

re.MatchObject

More Usage Examples

Compile a RegexObject for future reuse

let regex = re.compile("this(.+)that")

Matching a pattern from beginning

if let m = regex.match("this one is different from that") {
	m.group()  //"this one is different from that"
	m.group(1) //" one is different from "
}

Searching a pattern (first match)

if let m = regex.search("I want this one, not that one") {
	m.group()  //"this one, not that one"
	m.group(1) //" one, not "
}

Find all occurrences of a pattern

regex.findall("this or that, this and that") // ["this or that", "this and that"]

Get match results for all occurrences of a pattern

for m in regex.finditer("this or that, this and that") {
	m.group()  // 1st time: "this or that", 2nd time: "this and that"
	m.group(1) // 1st time: " or ", 2nd time: " and "
}

Splitting a string with pattern

let regex = re.compile("[\\+\\-\\*/]")

// By default, will split at all occurrences of the pattern
regex.split("1+2-3*4/5")    // ["1", "2", "3", "4", "5"]

// Setting a maxsplit = 2
regex.split("1+2-3*4/5", 2) // ["1", "2", "3*4/5"]

Replacing a pattern

let regex = re.compile("[Yy]ou")

// Replacing all occurrences (2 times in this example)
regex.sub("u", "You guys go grap your food")     // "u guys go grap ur food"
regex.subn("u", "You guys go grap your food")    // ("u guys go grap ur food", 2)

// Setting maximum replace count = 1 (1 times in this example)
regex.sub("u", "You guys go grap your food", 1)  // "u guys go grap your food"
regex.subn("u", "You guys go grap your food", 1) // ("u guys go grap your food", 1)

License

PySwiftyRegex is released under the MIT license. See LICENSE for details.

You might also like...
Swift Parser Combinators

Parsey Swift Parser Combinator Framework In addition to simple combinators, Parsey supports source location/range tracking, backtracking prevention, a

Great Swift String Pluralize Extension

Pluralize.swift Great Swift String Pluralize Extension case-insensitive tons of rules for irregular nouns (plural form) supports uncountable nouns all

An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.
An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.

PrediKit A Swift NSPredicate DSL for iOS & OS X inspired by SnapKit, lovingly written in Swift, and created by that weird dude at KrakenDev. If you're

Regular expressions for swift
Regular expressions for swift

Regex Advanced regular expressions for Swift Goals Regex library was mainly introduced to fulfill the needs of Swift Express - web application server

👩‍🎨 Elegant Attributed String composition in Swift sauce
👩‍🎨 Elegant Attributed String composition in Swift sauce

Elegant Attributed String composition in Swift sauce SwiftRichString is a lightweight library which allows to create and manipulate attributed strings

SwiftVerbalExpressions is a Swift library that helps to construct difficult regular expressions

SwiftVerbalExpressions Swift Regular Expressions made easy SwiftVerbalExpressions is a Swift library that helps to construct difficult regular express

A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.

PhoneNumberKit Swift 5.3 framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber. Features F

A comprehensive, lightweight string extension for Swift

SwiftString SwiftString is a lightweight string extension for Swift. This library was motivated by having to search StackOverflow for common string op

A Swift Formatter Kit
A Swift Formatter Kit

Format A Swift formatter kit. Simple formatting syntax for decimal numbers, currency, mass, addresses, ordinal numbers and hexadecimal colors. Usage I

Comments
  • findall working incorrectly

    findall working incorrectly

    http://stackoverflow.com/questions/35466584/swift-regex-find-all?noredirect=1#comment58632890_35466584

    Hi, have the following issue. Tried your library. But it returns incorrect results :(

    bug 
    opened by Arti3DPlayer 19
  • The `findAll` API have something wrong!

    The `findAll` API have something wrong!

    // Regex
    static let trendingDevListRegex = "<li class=\"d-sm-flex flex-justify(.|\n)+?</li>"
    static let trendingDevRegex = "<?li class=\"d-sm-flex flex-justify(.|\n)+?id=\"pa-(.+?)\"(.|\n)+?</li>"
    

    I use the trendingDevListRegex to pattern the Github Trending - Developers Page, and use the trendingDevRegex to pattern each one. And every match can be able to lose the first character just like the following screen shot:

    488fab0e-8c68-4e42-8625-73487299e560

    It was very strange !! 🤣 (The bad network refuse me to upload the image..wait for some time.)

    opened by Desgard 4
  • How to get function name given match string?

    How to get function name given match string?

    For example : https://gist.github.com/steve21124/c13c9203f1e5280611335032ada65a41

    1. match string "ImageStack.Notifications.stackDidReload" return "func subscribe()" 2.match string "self.cameraController.takePicture" return "takePicture()"

    is possible using this to get above?

    opened by steve21124 1
Releases(3.0.0)
Owner
Ce Zheng
Swifter who also writes C++
Ce Zheng
Texstyle allows you to format iOS attributed strings easily.

Texstyle allows you to format attributed strings easily. Features Applying attributes with strong typing and autocompletion Cache for attributes Subst

Rosberry 79 Sep 9, 2022
Easily show RichText(html) in SwiftUI

RichText LightMode DarkMode Code import SwiftUI

null 82 Jan 7, 2023
Easiest way to create an attributed UITextView (with support for multiple links and from html)

AttributedTextView Easiest way to create an attributed UITextView (with support for multiple links and html). See the demo app and the playground for

Edwin Vermeer 430 Nov 24, 2022
An easy way to add mentions to uitextview like Facebook and Instagram

OEMentions An easy way to add mentions to uitextview like Facebook and Instagram. It also include a tableview to show the users list to choose from. T

Omar Alessa 48 Oct 23, 2022
An easier way to compose attributed strings

TextAttributes makes it easy to compose attributed strings. let attrs = TextAttributes() .font(name: "HelveticaNeue", size: 16) .foregroundCol

Damien 2.2k Dec 31, 2022
BonMot is a Swift attributed string library

BonMot (pronounced Bon Mo, French for good word) is a Swift attributed string library. It abstracts away the complexities of the iOS, macOS, tvOS, and

Rightpoint 3.4k Dec 30, 2022
Croc is a swift emoji string parsing library

Croc is a library for parsing emojis on iOS. It provides a simple and lightweight interface for detecting, generating, categorizing and managing emoji characters, making emoji-powered features an easy task for developers.

Joe Kalash 127 Nov 20, 2022
Fully open source text editor for iOS written in Swift.

Edhita Fully open source text editor for iOS written in Swift. http://edhita.bornneet.com/ What Edhita means? Edhita (Romaji) == エディタ (Katakana) == Ed

Tatsuya Tobioka 1.2k Jan 1, 2023
A simple and customizable Markdown Parser for Swift

MarkdownKit MarkdownKit is a customizable and extensible Markdown parser for iOS and macOS. It supports many of the standard Markdown elements through

Bruno Oliveira 687 Dec 18, 2022
Marky Mark is a parser written in Swift that converts markdown into native views.

Marky Mark is a parser written in Swift that converts markdown into native views. The way it looks it highly customizable and the supported markdown syntax is easy to extend.

M2mobi 287 Nov 29, 2022