All Projects → LittleRockInGitHub → LLRegex

LittleRockInGitHub / LLRegex

Licence: MIT license
Regular expression library in Swift, wrapping NSRegularExpression.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to LLRegex

regexp-expand
Show the ELisp regular expression at point in rx form.
Stars: ✭ 18 (+0%)
Mutual labels:  regex, regular-expression
es6-template-regex
Regular expression for matching es6 template delimiters in a string.
Stars: ✭ 15 (-16.67%)
Mutual labels:  regex, regular-expression
learn-regex
Learn regex the easy way
Stars: ✭ 43,660 (+242455.56%)
Mutual labels:  regex, regular-expression
cregex
A small implementation of regular expression matching engine in C
Stars: ✭ 72 (+300%)
Mutual labels:  regex, regular-expression
Regex
🔤 Swifty regular expressions
Stars: ✭ 311 (+1627.78%)
Mutual labels:  regex, regular-expression
FileRenamerDiff
A File Renamer App featuring a difference display before and after the change.
Stars: ✭ 32 (+77.78%)
Mutual labels:  regex, regular-expression
compiler-design-lab
These are my programs for compiler design lab work in my sixth semester
Stars: ✭ 47 (+161.11%)
Mutual labels:  regex, regular-expression
Regex.persian.language
Collection of Regex for validating, filtering, sanitizing and finding Persian strings
Stars: ✭ 172 (+855.56%)
Mutual labels:  regex, regular-expression
termco
Regular Expression Counts of Terms and Substrings
Stars: ✭ 24 (+33.33%)
Mutual labels:  regex, regular-expression
pcre-heavy
A Haskell regular expressions library that doesn't suck | now on https://codeberg.org/valpackett/pcre-heavy
Stars: ✭ 52 (+188.89%)
Mutual labels:  regex, regular-expression
regex-comuns
Um estudo de regex comuns
Stars: ✭ 15 (-16.67%)
Mutual labels:  regex, regular-expression
RegexReplacer
A flexible tool to make complex replacements with regular expression
Stars: ✭ 38 (+111.11%)
Mutual labels:  regex, regular-expression
Regex For Regular Folk
🔍💪 Regular Expressions for Regular Folk — A visual, example-based introduction to RegEx [BETA]
Stars: ✭ 242 (+1244.44%)
Mutual labels:  regex, regular-expression
moar
Deterministic Regular Expressions with Backreferences
Stars: ✭ 19 (+5.56%)
Mutual labels:  regex, regular-expression
Regexpu
A source code transpiler that enables the use of ES2015 Unicode regular expressions in ES5.
Stars: ✭ 201 (+1016.67%)
Mutual labels:  regex, regular-expression
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (+94.44%)
Mutual labels:  regex, regular-expression
Regex Dos
👮 👊 RegEx Denial of Service (ReDos) Scanner
Stars: ✭ 143 (+694.44%)
Mutual labels:  regex, regular-expression
Grex
A command-line tool and library for generating regular expressions from user-provided test cases
Stars: ✭ 4,847 (+26827.78%)
Mutual labels:  regex, regular-expression
regex-not
Create a javascript regular expression for matching everything except for the given string.
Stars: ✭ 31 (+72.22%)
Mutual labels:  regex, regular-expression
doi-regex
Regular expression for matching DOIs
Stars: ✭ 28 (+55.56%)
Mutual labels:  regex, regular-expression

LLRegex

Build Status CocoaPods Compatible Platform

Regular expression library in Swift, wrapping NSRegularExpression. Don't hesitate to try out on playground.

Features

  • Value Semantics
  • Enumerates matches with Sequence
  • Named capture group (unavailable on iOS 8)
  • Range supported (NSRange eliminated)
  • Regex Options, Match Options
  • Find & Replace with flexibility
  • String matching, replacing, splitting

Communication

  • If you found a bug, open an issue, typically with related pattern.
  • If you have a feature request, open an issue.

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.1+
  • Swift 3.2

Installation

CocoaPods

pod 'LLRegex', '~> 1.3'

Swift Package Manager

dependencies: [
    .Package(url: "https://github.com/LittleRockInGitHub/LLRegex.git", majorVersion: 1)
]

Usage

Making a Regex

let numbers = Regex("(\\d)(\\d+)(\\d)")

let insensitive = Regex("LLRegex", options: [.caseInsensitive])

let runtimeError = Regex("")    // Runtime error would be raised

let invalid = try? Regex(pattern: "")   // nil returned

Searching

Method matches(in:options:range:) returns a sequence producing matches lazily, which is the only one for searching. All other variants were dropped thanks to the power of Sequence.

let s = "123-45-6789-0-123-45-6789-01234"
let subrange = s.characters.dropFirst(3).startIndex..<s.endIndex

for match in numbers.matches(in: s) {
    // enumerating
    match.matched
}

if let first = numbers.matches(in: s).first {
    // first match
    first.matched
}

let allMatches: [Match] = numbers.matches(in: s).all // all matches

let subrangeMatches = numbers.matches(in: s, options: [.withTransparentBounds], range: subrange)

for case let match in subrangeMatches.dropFirst(1) where match.matched != "6789" {
    match.matched
}

Match & Capture Groups

if let first = numbers.matches(in: s).first {
    
    first.matched
    first.range
    first.groups.count
    first.groups[1].matched
    first.groups[1].range
    
    let replacement = first.replacement(withTemplate: "$3$2$1")     // Replacement with template
}

Named Capture Group (unavailable on iOS 8)

Named capture group feature is enabled when .namedCaptureGroups is set.

let named = Regex("(?<year>\\d+)-(?<month>\\d+)-(?<day>\\d+)", options: .namedCaptureGroups)
let s = "Today is 2017-06-23."

for m in named.matches(in: s) {
    m.groups["year"]?.matched
}

named.replacingAllMatches(in: s, replacement: .replaceWithTemplate("${month}/${day}/${year}")) // Today is 06/23/2017.
  • Note: If the comment in pattern contains the notation of capture group, the detection for named capture group will fail.

Replacing

numbers.replacingFirstMatch(in: s, replacement: .remove)

numbers.replacingAllMatches(in: s, range: subrange, replacement: .replaceWithTemplate("$3$2$1"))

Flexible Find & Replace is offered by replacingMatches(in:options:range:replacing:).

numbers.replacingMatches(in: s) { (idx, match) -> Match.Replacing in
    
    switch idx {
    case 0:
        return .keep    // Keeps unchanged
    case 1:
        return .remove  // Removes the matched string
    case 2:
        return .replaceWithTemplate("($1-$3)")    // Replaces with template
    case 3:
        return .replaceWithString(String(match.matched.characters.reversed()))   // Replaces with string
    default:
        return .stop    // Stops replacing
    }
}

String Matching

"123".isMatching("\\d+")
"llregex".isMatching(insensitive)   // Regex is accepted
"123-456".isMatching("\\d+")    // isMatching(_:) checks whether matches entirely

String Replacing

  • Note: Two variants - one with pattern and template label, the other is not.
"123".replacingAll("1", with: "$0")
"123-321".replacingAll(pattern: "(\\d)(\\d)", withTemplate: "$2$1")

s.replacingFirst(pattern: numbers, in: subrange, withTemplate: "!")

String Splitting

s.split(seperator: "\\d")
s.split(seperator: numbers, maxSplits: 2, omittingEmptyString: false)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].