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

Overview

SwiftVerbalExpressions

Build Status Carthage compatible

Swift Regular Expressions made easy

SwiftVerbalExpressions is a Swift library that helps to construct difficult regular expressions - ported from the awesome JavaScript VerbalExpressions.

Examples

Here's a couple of simple examples to give an idea of how VerbalExpressions works:

Testing if we have a valid URL

// Create an example of how to test for correctly formed URLs
let tester = VerEx()
    .startOfLine()
    .then("http")
    .maybe("s")
    .then("://")
    .maybe("www")
    .anythingBut(" ")
    .endOfLine()

// Create an example URL
let testMe = "https://www.google.com"

// Use test() method
if tester.test(testMe) {
    print("We have a correct URL") // This output will fire
}
else {
    print("The URL is incorrect")
}

// Use =~ operator
if testMe =~ tester {
    print("We have a correct URL") // This output will fire
}
else {
    print("The URL is incorrect")
}

prin(tester) // Outputs the actual expression used: "^(?:http)(?:s)?(?::\/\/)(?:www)?(?:[^ ]*)$"

Replacing strings

let replaceMe = "Replace bird with a duck"

// Create an expression that seeks for word "bird"
let verEx = VerEx().find("bird")

// Execute the expression like a normal RegExp object
let result = verEx.replace(replaceMe, with: "duck")

print(result) // Outputs "Replace duck with a duck"

Shorthand for string replace:

let result2 = VerEx().find("red").replace("We have a red house", with: "blue")

print(result2) // Outputs "We have a blue house"

API documentation

You can find the documentation for the original JavaScript repo on their wiki.

Contributions

Clone the repo and fork! Pull requests are warmly welcome!

Thanks!

Thank you to @jehna for coming up with the awesome original idea!
Thank you to @kishikawakatsumi for ObjectiveCVerbalExpressions from which I borrowed some code!

Other implementations

You can view all implementations on VerbalExpressions.github.io

Installation and use

This version is under testing, but it supports Swift Package Manager. Therefore it can be included in the project with:

        .package(
            url: "https://github.com/VerbalExpressions/SwiftVerbalExpressions.git",
            from: "

And:

        .target(
            name: "YourProject",
            dependencies: ["VerbalExpressions"]),
Comments
  • use struct instead of class, overloading operators

    use struct instead of class, overloading operators

    1. Updated Class to be a Struct instead. This makes the whole thing immutable, and get value semantics. A regular expression is clearly 'data' and should have value semantics.
    2. Added two more functions for the custom operators, which just allow you to reverse the arguments. if a =~ b is true, b =~ a should also be true. This can be done because string matching is commutative.
    opened by nmn 15
  • Test Syntax Error in Swift 4

    Test Syntax Error in Swift 4

    Somewhere along the line, NSRegularExpressonOptions became NSRegularExpression.Options, which is a struct instead of a class. This means that a few of VerbalExpressionss methods are mutating now that .options has value semantics instead of reference semantics. This, in turn, causes a syntax error in the testSearchOneLine() test (on line 197) because it declares tester as a let, but the searchOneLine() method is one of the newly-mutating methods.

    I see three ways around this:

    1. Make tester a var instead of a let, and just deal with having mutating functions.
    2. Make VerbalExpressions a class instead of a struct.
    3. Make .options a wrapper class for the "real" options.

    (Personally, I'm torn between 2 & 3)

    Anyway, I can submit a PR with everything updated to Swift 3/4 syntax (it compiles & tests under both language settings). Just let me know how you want this fixed.

    opened by TheOtherDave 1
  • Swift 3 Syntax

    Swift 3 Syntax

    All I did was run the migrator and fix all but one of the warnings. There's an unused return value in one of the tests. I'm not sure if it's a left-over line of code, or if the function should get the @discardableResult annotation.

    opened by TheOtherDave 1
  • Missing features:

    Missing features:

    Fixed in #5

    • [x] Can't use another VerbalExpression as an argument in a VerbalExpression Chain. This is a common use-case explained in the original JS version of the library.
    • [x] No .or method
    opened by nmn 0
  • [Question] Is it possible to locate between strings?

    [Question] Is it possible to locate between strings?

    Are lazy quantifiers like these (?s)(?<=---).*(?=---) supported in VerEx?

    I wanted to extract any text between two sets of '---' in a string, is it possible?

    Thank you.

    opened by lf-araujo 0
Releases(0.0.2)
Owner
null
A Cross-Platform String and Regular Expression Library written in Swift.

Guitar ?? A Cross-Platform String and Regular Expression Library written in Swift. About This library seeks to add common string manipulation function

Arthur Ariel Sabintsev 659 Dec 27, 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
🌭 Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it.

Mustard ?? Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it. Quick start using character sets Foundation

Mathew Sanders 695 Nov 11, 2022
Swift markdown library

Markdown ![Swift version](https://img.shields.io/badge/Swift-2.1 | 2.2-blue.svg) ![GitHub license](https://img.shields.io/badge/license-LGPL v3-green.

Crossroad Labs 79 Oct 9, 2022
Lightweight library to set an Image as text background. Written in swift.

![](https://img.shields.io/badge/Swift 2-compatible-4BC51D.svg?style=flat-square) Simple and light weight UIView that animate text with an image. Demo

Lucas Ortis 552 Sep 9, 2022
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

Joe Kalash 125 Sep 27, 2021
A library for formatting strings on iOS and macOS

Sprinter Introduction What? Why? How? Usage Installation Integration Localization Thread Safety Advanced Usage Introduction What? Sprinter is a librar

Nick Lockwood 168 Feb 6, 2022
A simple library for building attributed strings, for a more civilized age.

Veneer A simple library for building attributed strings, for a more civilized age. Veneer was created to make creating attributed strings easier to re

Wess Cope 26 Dec 27, 2022
SZMentionsSwift is a lightweight mentions library for iOS.

SZMentionsSwift is a lightweight mentions library for iOS. This library was built to assist with the adding, removing and editing of a mention within a textview.

Steven Zweier 122 Dec 12, 2022
A simple library that provides standard Unicode emoji support across all platforms

Twitter Emoji (Twemoji) A simple library that provides standard Unicode emoji support across all platforms. Twemoji v13.1 adheres to the Unicode 13.0

Twitter 15k Jan 8, 2023
iOS port from libphonenumber (Google's phone number handling library)

libPhoneNumber for iOS NBPhoneNumberUtil NBAsYouTypeFormatter ARC only Update Log https://github.com/iziz/libPhoneNumber-iOS/wiki/Update-Log Issue You

iziz 2.3k Jan 3, 2023
User input masking library repo.

Migration Guide: v.6 This update brings breaking changes. Namely, the autocomplete flag is now a part of the CaretGravity enum, thus the Mask::apply c

red_mad_robot 548 Dec 20, 2022
A lightweight fuzzy-search library, with zero dependencies

Fuse What is Fuse? Fuse is a super lightweight library which provides a simple way to do fuzzy searching. Usage Example 1 let fuse = Fuse() let result

Kiro Risk 864 Dec 29, 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
Swift Parser Combinators

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

Richard Wei 56 Jun 30, 2022
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

Joshua Arvin Lat 193 Nov 8, 2022