All Projects → crossroadlabs → Regex

crossroadlabs / Regex

Licence: apache-2.0
Regular expressions for swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Regex

dregex
Dregex is a JVM library that implements a regular expression engine using deterministic finite automata (DFA). It supports some Perl-style features and yet retains linear matching time, and also offers set operations.
Stars: ✭ 37 (-87.91%)
Mutual labels:  regex, regular-expression
RgxGen
Regex: generate matching and non matching strings based on regex pattern.
Stars: ✭ 45 (-85.29%)
Mutual labels:  regex, regular-expression
expand-brackets
Expand POSIX bracket expressions (character classes) in glob patterns.
Stars: ✭ 26 (-91.5%)
Mutual labels:  regex, regular-expression
pcre-net
PCRE.NET - Perl Compatible Regular Expressions for .NET
Stars: ✭ 114 (-62.75%)
Mutual labels:  regex, regular-expression
Anymatch
‼️ Matches strings against configurable strings, globs, regular expressions, and/or functions
Stars: ✭ 289 (-5.56%)
Mutual labels:  regex, regular-expression
LLRegex
Regular expression library in Swift, wrapping NSRegularExpression.
Stars: ✭ 18 (-94.12%)
Mutual labels:  regex, regular-expression
cheat-sheet-pdf
📜 A Cheat-Sheet Collection from the WWW
Stars: ✭ 728 (+137.91%)
Mutual labels:  regex, regular-expression
regexp-expand
Show the ELisp regular expression at point in rx form.
Stars: ✭ 18 (-94.12%)
Mutual labels:  regex, regular-expression
Re Flex
The regex-centric, fast lexical analyzer generator for C++ with full Unicode support. Faster than Flex. Accepts Flex specifications. Generates reusable source code that is easy to understand. Introduces indent/dedent anchors, lazy quantifiers, functions for lex/syntax error reporting, and more. Seamlessly integrates with Bison and other parsers.
Stars: ✭ 274 (-10.46%)
Mutual labels:  regex, regular-expression
regex-cache
Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in dramatic speed improvements.
Stars: ✭ 39 (-87.25%)
Mutual labels:  regex, regular-expression
Rex
Your RegEx companion.
Stars: ✭ 283 (-7.52%)
Mutual labels:  regex, regular-expression
genex
Genex package for Go
Stars: ✭ 64 (-79.08%)
Mutual labels:  regex, regular-expression
es6-template-regex
Regular expression for matching es6 template delimiters in a string.
Stars: ✭ 15 (-95.1%)
Mutual labels:  regex, regular-expression
CVparser
CVparser is software for parsing or extracting data out of CV/resumes.
Stars: ✭ 28 (-90.85%)
Mutual labels:  regex, regular-expression
termco
Regular Expression Counts of Terms and Substrings
Stars: ✭ 24 (-92.16%)
Mutual labels:  regex, regular-expression
globrex
Glob to regular expression with support for extended globs.
Stars: ✭ 52 (-83.01%)
Mutual labels:  regex, regular-expression
cregex
A small implementation of regular expression matching engine in C
Stars: ✭ 72 (-76.47%)
Mutual labels:  regex, regular-expression
Regex
🔤 Swifty regular expressions
Stars: ✭ 311 (+1.63%)
Mutual labels:  regex, regular-expression
pamatcher
A pattern matching library for JavaScript iterators
Stars: ✭ 23 (-92.48%)
Mutual labels:  regex, regular-expression
hex-color-regex
Regular expression for matching hex color values from string.
Stars: ✭ 29 (-90.52%)
Mutual labels:  regex, regular-expression

by Crossroad Labs

Regex

🐧 linux: ready GitHub license 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 ;)

Features

  • [x] Deep Integration with Swift
    • [x] =~ operator support
    • [x] Swift Pattern Matching (aka switch operator) support
  • [x] Named groups
  • [x] Match checking
  • [x] Extraction/Search functions
  • [x] Replace functions
    • [x] With a pattern
    • [x] With custom replacing function
  • [x] Splitting with a Regular Expression
    • [x] Simple
    • [x] With groups
  • [x] String extensions
  • [x] Supports grapheme clusters 👨‍👩‍👧

Extra

Path to Regex converter is available as a separate library here: PathToRegex

This one allows using path patterns like /folder/*/:file.txt or /route/:one/:two to be converted to Regular Expressions and matched against strings.

Getting started

Installation

Package Manager

Add the following dependency to your Package.swift:

.Package(url: "https://github.com/crossroadlabs/Regex.git", majorVersion: 1)

Run swift build and build your app.

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" =~ "(.+?)([123]*)(.*)".r
"l321321alala" =~ "(.+?)([123]*)(.*)"

//similar function
"(.+?)([123]*)(.*)".r!.matches("l321321alala")

Operator !~ returns true if expression does NOT match:

"l321321alala" !~ "(.+?)([123]*)(.*)".r
"l321321alala" !~ "(.+?)([123]*)(.*)"
//both return false

Swift Pattern Matching (aka switch keyword)

Regex provides very deep integration with Swift and can be used with the switch keyword in the following way:

let letter = "a"
let digit = "1"
let other = "!"

//you just put your string is a regular Swift's switch to match to regular expressions
switch letter {
	//note .r after the string literal of the pattern
	case "\\d".r: print("digit")
	case "[a-z]".r: print("letter")
	default: print("bizarre symbol")
}

switch digit {
	case "\\d".r: print("digit")
	case "[a-z]".r: print("letter")
	default: print("bizarre symbol")
}

switch other {
	//note .r after the string literal of the pattern
	case "\\d".r: print("digit")
	case "[a-z]".r: print("letter")
	default: print("bizarre symbol")
}

The output of the code above will be:

letter
digit
bizarre symbol

Accessing groups:

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

Named groups:

let regex:RegexType = try Regex(pattern:"(.+?)([123]*)(.*)",
                                        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 = "(.+?)([123]*)(.*)".r?.replaceAll(in: "l321321alala", with: "$1-$2-$3")
//replaced is "l-321321-alala"

Replace with custom replacer function:

let replaced = "(.+?)([123]+)(.+?)".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", "."]

Changelog

You can view the CHANGELOG as a separate document here.

Contributing

To get started, sign the Contributor License Agreement.

Crossroad Labs by Crossroad Labs

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].