All Projects → DavidSkrundz → Regex

DavidSkrundz / Regex

Licence: LGPL-3.0 License
A pure Swift NFA implementation of a regular expression engine

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Regex

moar
Deterministic Regular Expressions with Backreferences
Stars: ✭ 19 (-29.63%)
Mutual labels:  regex, regular-expression-engine
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 (+37.04%)
Mutual labels:  regex, regular-expression-engine
one-more-re-nightmare
A fast regular expression compiler in Common Lisp
Stars: ✭ 104 (+285.19%)
Mutual labels:  regex, regular-expression-engine
DFIRRegex
A repo to centralize some of the regular expressions I've found useful over the course of my DFIR career.
Stars: ✭ 33 (+22.22%)
Mutual labels:  regex
pamatcher
A pattern matching library for JavaScript iterators
Stars: ✭ 23 (-14.81%)
Mutual labels:  regex
Pawn.Regex
🔎 Plugin that adds support for regular expressions in Pawn
Stars: ✭ 34 (+25.93%)
Mutual labels:  regex
rewrite-imports
Rewrite `import` statements as `require()`s; via RegExp
Stars: ✭ 31 (+14.81%)
Mutual labels:  regex
RxValidationTextInputLayout
The easiest way to bring validation to your project
Stars: ✭ 45 (+66.67%)
Mutual labels:  regex
genex
Genex package for Go
Stars: ✭ 64 (+137.04%)
Mutual labels:  regex
rust-regex-playground
Web tool to evaluate rust regular expressions
Stars: ✭ 13 (-51.85%)
Mutual labels:  regex
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 (+44.44%)
Mutual labels:  regex
retrie
Efficient Trie-based regex unions for blacklist/whitelist filtering and one-pass mapping-based string replacing
Stars: ✭ 35 (+29.63%)
Mutual labels:  regex
regex
A set of ready-made regex helper methods for use in your Laravel application.
Stars: ✭ 226 (+737.04%)
Mutual labels:  regex
stringx
Drop-in replacements for base R string functions powered by stringi
Stars: ✭ 14 (-48.15%)
Mutual labels:  regex
extract-email-address
Extracts email address from an arbitrary text input.
Stars: ✭ 45 (+66.67%)
Mutual labels:  regex
RegExp-Learning
学习正则表达式
Stars: ✭ 30 (+11.11%)
Mutual labels:  regex
String.prototype.matchAll
Spec-compliant polyfill for String.prototype.matchAll, in ES2020
Stars: ✭ 14 (-48.15%)
Mutual labels:  regex
log4stash
Module to Log log4net Messages to ElasticSearch
Stars: ✭ 60 (+122.22%)
Mutual labels:  regex
i3blocks-modules
Custom modules for i3blocks status bar
Stars: ✭ 36 (+33.33%)
Mutual labels:  regex
crystular
Crystal regex tester http://www.crystular.org/
Stars: ✭ 31 (+14.81%)
Mutual labels:  regex

Regex (V2 WIP) Swift Version Platforms Build Status Codebeat Status Codecov

A pure Swift implementation of a Regular Expression Engine

Trying again with V2 using DFAs instead of NFAs to get grep-like performance

Usage

To avoid compiling overhead it is possible to create a Regex instance

// Compile the expression
let regex = try! Regex(pattern: "[a-zA-Z]+")

let string = "RegEx is tough, but useful."

// Search for matches
let words = regex.match(string)

/*
words = [
	RegexMatch(match: "RegEx", groups: []),
	RegexMatch(match: "is", groups: []),
	RegexMatch(match: "tough", groups: []),
	RegexMatch(match: "but", groups: []),
	RegexMatch(match: "useful", groups: []),
]
*/

If compiling overhead is not an issue it is possible to use the =~ operator to match a string

let fourLetterWords = "drink beer, it's very nice!" =~ "\\b\\w{4}\\b" ?? []

/*
fourLetterWords = [
	RegexMatch(match: "beer", groups: []),
	RegexMatch(match: "very", groups: []),
	RegexMatch(match: "nice", groups: []),
]
*/

By default the Global flag is active. To change which flag are active, add a / at the start of the pattern, and add /<flags> at the end. The available flags are:

  • g Global - Allows multiple matches
  • i Case Insensitive - Case insensitive matching
  • m Multiline - ^ and $ also match the begining and end of a line
// Global and Case Insensitive search
let regex = try! Regex(pattern: "/\\w+/ig")

Supported Operations

Character Classes

Pattern Description Supported
. [^\n\r]
[^] [\s\S]
\w [A-Za-z0-9_]
\W [^A-Za-z0-9_]
\d [0-9]
\D [^0-9]
\s [\ \r\n\t\v\f]
\S [^\ \r\n\t\v\f]
[ABC] Any in the set
[^ABC] Any not in the set
[A-Z] Any in the range inclusively

Anchors (Match positions not characters)

Pattern Description Supported
^ Beginning of string
$ End of string
\b Word boundary
\B Not word boundary

Escaped Characters

Pattern Description Supported
\0 Octal escaped character
\00 Octal escaped character
\000 Octal escaped character
\xFF Hex escaped character
\uFFFF Unicode escaped character
\cA Control character
\t Tab
\n Newline
\v Vertical tab
\f Form feed
\r Carriage return
\0 Null
\. .
\\ \
\+ +
\* *
\? ?
\^ ^
\$ $
\{ {
\} }
\[ [
\] ]
\( (
\) )
\/ /
| ` `

Groups and Lookaround

Pattern Description Supported
(ABC) Capture group
(<name>ABC) Named capture group
\1 Back reference
\'name' Named back reference
(?:ABC) Non-capturing group
(?=ABC) Positive lookahead
(?!ABC) Negative lookahead
(?<=ABC) Positive lookbehind
(?<!ABC) Negative lookbehing

Greedy Quantifiers

Pattern Description Supported
+ One or more
* Zero or more
? Optional
{n} n
{,} Same as *
{,n} n or less
{n,} n or more
{n,m} n to m

Lazy Quantifiers

Pattern Description Supported
+? One or more
*? Zero or more
?? Optional
{n}? n
{,n}? n or less
{n,}? n or more
{n,m}? n to m

Alternation

Pattern Description Supported
| Everything before or everything after

Flags

Pattern Description Supported
i Case insensitive
g Global
m Multiline

Inner Workings

(Similar to before)

  • Lexer (String input to Tokens)
  • Parser (Tokens to NFA)
  • Compiler (NFA to DFA)
  • Optimizer (Simplify DFA (eg. char(a), char(b) -> string(ab)) for better performance)
  • Engine (Matches an input String using the DFA)

Note

Swift treats \r\n as a single Character. Use \n\r to have both.

Resources

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