All Projects → spatie → Regex

spatie / Regex

Licence: mit
A sane interface for php's built in preg_* functions

Projects that are alternatives of or similar to Regex

Guitar
A Cross-Platform String and Regular Expression Library written in Swift.
Stars: ✭ 641 (-29.48%)
Mutual labels:  regex, regular-expression
Shallow Clone
Make a shallow clone of an object, array or primitive.
Stars: ✭ 23 (-97.47%)
Mutual labels:  regex, regular-expression
Generex
A Java library for generating String from a regular expression.
Stars: ✭ 316 (-65.24%)
Mutual labels:  regex, regular-expression
Rex
Your RegEx companion.
Stars: ✭ 283 (-68.87%)
Mutual labels:  regex, regular-expression
Chinamobilephonenumberregex
Regular expressions that match the mobile phone number in mainland China. / 一组匹配中国大陆手机号码的正则表达式。
Stars: ✭ 4,440 (+388.45%)
Mutual labels:  regex, regular-expression
Anymatch
‼️ Matches strings against configurable strings, globs, regular expressions, and/or functions
Stars: ✭ 289 (-68.21%)
Mutual labels:  regex, regular-expression
Regexp2
A full-featured regex engine in pure Go based on the .NET engine
Stars: ✭ 389 (-57.21%)
Mutual labels:  regex, regular-expression
genex
Genex package for Go
Stars: ✭ 64 (-92.96%)
Mutual labels:  regex, regular-expression
Commonregex
🍫 A collection of common regular expressions for Go
Stars: ✭ 733 (-19.36%)
Mutual labels:  regex, regular-expression
Regexplain
🔍 An RStudio addin slash regex utility belt
Stars: ✭ 413 (-54.57%)
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 (-69.86%)
Mutual labels:  regex, regular-expression
Onigmo
Onigmo is a regular expressions library forked from Oniguruma.
Stars: ✭ 536 (-41.03%)
Mutual labels:  regex, regular-expression
pcre-net
PCRE.NET - Perl Compatible Regular Expressions for .NET
Stars: ✭ 114 (-87.46%)
Mutual labels:  regex, regular-expression
Regex
Regular expressions for swift
Stars: ✭ 306 (-66.34%)
Mutual labels:  regex, regular-expression
extglob
Extended globs. Add (almost) the expressive power of regular expressions to glob patterns.
Stars: ✭ 25 (-97.25%)
Mutual labels:  regex, regular-expression
Minta
✳️  Electron app for generating regular expressions
Stars: ✭ 353 (-61.17%)
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 (-95.71%)
Mutual labels:  regex, regular-expression
hex-color-regex
Regular expression for matching hex color values from string.
Stars: ✭ 29 (-96.81%)
Mutual labels:  regex, regular-expression
Picomatch
Blazing fast and accurate glob matcher written JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
Stars: ✭ 393 (-56.77%)
Mutual labels:  regex, regular-expression
Regulex
🚧 Regular Expression Excited!
Stars: ✭ 4,877 (+436.52%)
Mutual labels:  regex, regular-expression

Making regex great again

Latest Version on Packagist Tests Software License Total Downloads

Php's built in preg_* functions require some odd patterns like passing variables by reference and treating false or null values as errors. spatie/regex provides a cleaner interface for preg_match, preg_match_all, preg_replace and preg_replace_callback.

use Spatie\Regex\Regex;

// Using `match`
Regex::match('/a/', 'abc'); // `MatchResult` object
Regex::match('/a/', 'abc')->hasMatch(); // true
Regex::match('/a/', 'abc')->result(); // 'a'

// Capturing groups with `match`
Regex::match('/a(b)/', 'abc')->result(); // 'ab'
Regex::match('/a(b)/', 'abc')->group(1); // 'b'

// Setting defaults
Regex::match('/a(b)/', 'xyz')->resultOr('default'); // 'default'
Regex::match('/a(b)/', 'xyz')->groupOr(1, 'default'); // 'default'

// Using `matchAll`
Regex::matchAll('/a/', 'abcabc')->hasMatch(); // true
Regex::matchAll('/a/', 'abcabc')->results(); // Array of `MatchResult` objects

// Using replace
Regex::replace('/a/', 'b', 'abc')->result(); // 'bbc';
Regex::replace('/a/', function (MatchResult $result) {
    return $result->result() . 'Hello!';
}, 'abc')->result(); // 'aHello!bc';

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/regex

Usage

Matching a pattern once

Matches a pattern on a subject. Returns a MatchResult object for the first match.

/**
 * @param string $pattern
 * @param string $subject
 *
 * @return \Spatie\Regex\MatchResult
 */
Regex::match(string $pattern, string $subject): MatchResult

MatchResult::hasMatch(): bool

Checks if the pattern matches the subject.

Regex::match('/abc/', 'abc')->hasMatch(); // true
Regex::match('/def/', 'abc')->hasMatch(); // false

MatchResult::result(): string

Return the full match that was made. Returns null if no match was made.

Regex::match('/abc/', 'abc')->result(); // 'abc'
Regex::match('/def/', 'abc')->result(); // null

MatchResult::group(int $id): string

Return the contents of a captured group (with a 1-based index). Throws a RegexFailed exception if the group doesn't exist.

Regex::match('/a(b)c/', 'abc')->group(1); // 'b'
Regex::match('/a(b)c/', 'abc')->group(2); // `RegexFailed` exception

Matching all occurences of a pattern

Matches a pattern on a subject. Returns a MatchAllResult object containing all matches.

/**
 * @param string $pattern
 * @param string $subject
 *
 * @return \Spatie\Regex\MatchAllResult
 */
public static function matchAll(string $pattern, string $subject): MatchAllResult

MatchAllResult::hasMatch(): bool

Checks if the pattern matches the subject.

Regex::matchAll('/abc/', 'abc')->hasMatch(); // true
Regex::matchAll('/abc/', 'abcabc')->hasMatch(); // true
Regex::matchAll('/def/', 'abc')->hasMatch(); // false

MatchAllResult::results(): array

Returns an array of MatchResult objects.

$results = Regex::matchAll('/ab([a-z])/', 'abcabd')->results();

$results[0]->result(); // 'abc'
$results[0]->group(1); // 'c'
$results[1]->result(); // 'abd'
$results[1]->group(1); // 'd'

Replacing a pattern in a subject

Replaces a pattern in a subject. Returns a ReplaceResult object.

/**
 * @param string|array $pattern
 * @param string|array|callable $replacement
 * @param string|array $subject
 * @param int $limit
 *
 * @return \Spatie\Regex\ReplaceResult
 */
public static function replace($pattern, $replacement, $subject, $limit = -1): ReplaceResult

ReplaceResult::result(): mixed

Regex::replace('/a/', 'b', 'abc')->result(); // 'bbc'

Regex::replace also works with callables. The callable will receive a MatchResult instance as it's argument.

Regex::replace('/a/', function (MatchResult $matchResult) {
    return str_repeat($matchResult->result(), 2);
}, 'abc')->result(); // 'aabc'

Patterns, replacements and subjects can also be arrays. Regex::replace behaves exactly like preg_replace in those instances.

Error handling

If anything goes wrong in a Regex method, a RegexFailed exception gets thrown. No need for checking preg_last_error().

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.

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