All Projects → sindresorhus → Regex

sindresorhus / Regex

Licence: MIT license
🔤 Swifty regular expressions

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Regex

cregex
A small implementation of regular expression matching engine in C
Stars: ✭ 72 (-76.85%)
Mutual labels:  regex, regular-expression
regex-comuns
Um estudo de regex comuns
Stars: ✭ 15 (-95.18%)
Mutual labels:  regex, regular-expression
Regexpu
A source code transpiler that enables the use of ES2015 Unicode regular expressions in ES5.
Stars: ✭ 201 (-35.37%)
Mutual labels:  regex, regular-expression
regex
Regular expressions for Prolog
Stars: ✭ 16 (-94.86%)
Mutual labels:  regex, regular-expression
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (-88.75%)
Mutual labels:  regex, regular-expression
Grex
A command-line tool and library for generating regular expressions from user-provided test cases
Stars: ✭ 4,847 (+1458.52%)
Mutual labels:  regex, regular-expression
RegexReplacer
A flexible tool to make complex replacements with regular expression
Stars: ✭ 38 (-87.78%)
Mutual labels:  regex, regular-expression
Js Regular Expression Awesome
📄我收藏的正则表达式大全,欢迎补充
Stars: ✭ 120 (-61.41%)
Mutual labels:  regex, regular-expression
learn-regex
Learn regex the easy way
Stars: ✭ 43,660 (+13938.59%)
Mutual labels:  regex, regular-expression
moar
Deterministic Regular Expressions with Backreferences
Stars: ✭ 19 (-93.89%)
Mutual labels:  regex, regular-expression
doi-regex
Regular expression for matching DOIs
Stars: ✭ 28 (-91%)
Mutual labels:  regex, regular-expression
regex-not
Create a javascript regular expression for matching everything except for the given string.
Stars: ✭ 31 (-90.03%)
Mutual labels:  regex, regular-expression
Regex Dos
👮 👊 RegEx Denial of Service (ReDos) Scanner
Stars: ✭ 143 (-54.02%)
Mutual labels:  regex, regular-expression
Regex.persian.language
Collection of Regex for validating, filtering, sanitizing and finding Persian strings
Stars: ✭ 172 (-44.69%)
Mutual labels:  regex, regular-expression
Micromatch
Contributing Pull requests and stars are always welcome. For bugs and feature requests, please create an issue. Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Stars: ✭ 1,979 (+536.33%)
Mutual labels:  regex, regular-expression
Regex For Regular Folk
🔍💪 Regular Expressions for Regular Folk — A visual, example-based introduction to RegEx [BETA]
Stars: ✭ 242 (-22.19%)
Mutual labels:  regex, regular-expression
To Regex Range
Pass two numbers, get a regex-compatible source string for matching ranges. Fast compiler, optimized regex, and validated against more than 2.78 million test assertions. Useful for creating regular expressions to validate numbers, ranges, years, etc.
Stars: ✭ 97 (-68.81%)
Mutual labels:  regex, regular-expression
Orchestra
One language to be RegExp's Successor. Visually readable and rich, technically safe and extended, naturally scalable, advanced, and optimized
Stars: ✭ 103 (-66.88%)
Mutual labels:  regex, regular-expression
FileRenamerDiff
A File Renamer App featuring a difference display before and after the change.
Stars: ✭ 32 (-89.71%)
Mutual labels:  regex, regular-expression
compiler-design-lab
These are my programs for compiler design lab work in my sixth semester
Stars: ✭ 47 (-84.89%)
Mutual labels:  regex, regular-expression

Regex

Swifty regular expressions

This is a wrapper for NSRegularExpression that makes it more convenient and type-safe to use regular expressions in Swift.

Install

Add the following to Package.swift:

.package(url: "https://github.com/sindresorhus/Regex", from: "0.1.0")

Or add the package in Xcode.

Usage

First, import the package:

import Regex

Supported regex syntax.

Examples

Check if it matches:

Regex(#"\d+"#).isMatched(by: "123")
//=> true

Get first match:

Regex(#"\d+"#).firstMatch(in: "123-456")?.value
//=> "123"

Get all matches:

Regex(#"\d+"#).allMatches(in: "123-456").map(\.value)
//=> ["123", "456"]

Replacing first match:

"123🦄456".replacingFirstMatch(of: #"\d+"#, with: "")
//=> "🦄456"

Replacing all matches:

"123🦄456".replacingAllMatches(of: #"\d+"#, with: "")
//=> "🦄"

Named capture groups:

let regex = Regex(#"\d+(?<word>[a-z]+)\d+"#)

regex.firstMatch(in: "123unicorn456")?.group(named: "word")?.value
//=> "unicorn"

Pattern matching:

switch "foo123" {
case Regex(#"^foo\d+$"#):
	print("Match!")
default:
	break
}

switch Regex(#"^foo\d+$"#) {
case "foo123":
	print("Match!")
default:
	break
}

Multiline and comments:

let regex = Regex(
	#"""
	^
	[a-z]+  # Match the word
	\d+     # Match the number
	$
	"""#,
	options: .allowCommentsAndWhitespace
)

regex.isMatched(by: "foo123")
//=> true

API

See the API docs.

FAQ

Why are pattern strings wrapped in #?

Those are raw strings and they make it possible to, for example, use \d without having to escape the backslash.

Related

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