All Projects → SimpleRegex → Srl Php

SimpleRegex / Srl Php

Licence: mit
Simple Regex Language

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Srl Php

Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+22.35%)
Mutual labels:  composer, composer-packages
video-downloader
Video Downloader for Facebook.
Stars: ✭ 63 (-96.52%)
Mutual labels:  composer, composer-packages
Laravel Messenger
Simple user messaging package for Laravel
Stars: ✭ 2,140 (+18.36%)
Mutual labels:  composer, composer-packages
Packages
Enhances Composer Satis with webhook integrations to GitHub and GitLab
Stars: ✭ 157 (-91.32%)
Mutual labels:  composer, composer-packages
Regexanalyzer
Regular Expression Analyzer and Composer for Node.js / XPCOM / Browser Javascript, PHP, Python
Stars: ✭ 29 (-98.4%)
Mutual labels:  composer, regular-expression
Composer Patches
Applies a patch from a local or remote file to any package that is part of a given composer project. Patches can be defined both on project and on package level. Optional support for patch versioning, sequencing, custom patch applier configuration and composer command for testing/troubleshooting patches.
Stars: ✭ 196 (-89.16%)
Mutual labels:  composer, composer-packages
tdee-calculator
TDEE Calculator is a composer library that calculates how much energy (calories) are burned daily given the weight, height and age or Lean Body Mass.
Stars: ✭ 16 (-99.12%)
Mutual labels:  composer, composer-packages
Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (-82.36%)
Mutual labels:  composer, composer-packages
Awesome Composer
😎 A curated awesome list for Composer, Packagist, Satis, Plugins, Scripts, Composer related resources, tutorials.
Stars: ✭ 738 (-59.18%)
Mutual labels:  composer, composer-packages
Laravel Server Monitor
Server Monitoring Command for Laravel Applications
Stars: ✭ 424 (-76.55%)
Mutual labels:  composer, composer-packages
Composer Custom Directory Installer
A composer plugin, to install differenty types of composer packages in custom directories outside the default composer default installation path which is in the vendor folder.
Stars: ✭ 117 (-93.53%)
Mutual labels:  composer, composer-packages
Rexrex
🦖 Composable JavaScript regular expressions
Stars: ✭ 34 (-98.12%)
Mutual labels:  composer, regular-expression
Satis Control Panel
Satis Control Panel (SCP) is a simple web UI for managing your Satis Repository for Composer Packages.
Stars: ✭ 144 (-92.04%)
Mutual labels:  composer, composer-packages
Composer
⚠️ ⚠️ ⚠️ Hyperledger Composer has been deprecated ⚠️ ⚠️ ⚠️
Stars: ✭ 1,676 (-7.3%)
Mutual labels:  composer
Regex Dos
👮 👊 RegEx Denial of Service (ReDos) Scanner
Stars: ✭ 143 (-92.09%)
Mutual labels:  regular-expression
Wayeb
Wayeb is a Complex Event Processing and Forecasting (CEP/F) engine written in Scala.
Stars: ✭ 138 (-92.37%)
Mutual labels:  regular-expression
Lightning Project
A Composer-based installer for the Lightning distribution of Drupal 8.
Stars: ✭ 137 (-92.42%)
Mutual labels:  composer
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+16.76%)
Mutual labels:  query-builder
Laravel Awesome
Laravel 学习图谱
Stars: ✭ 143 (-92.09%)
Mutual labels:  composer
Foxy
A fast, reliable, and secure NPM/Yarn bridge for Composer
Stars: ✭ 137 (-92.42%)
Mutual labels:  composer

Simple Regex Language

codecov Build Status License

We all know Regular Expressions are hard to read. Once written you're happy if you never ever have to touch this line of code again because you're going to have a hard time understanding what you've written there.

Before we get started, a short note on how to use SRL: You can either use this project directly, or, if you're not into PHP or including a library like that, you can build your query online and use the generated Regular Expression elsewhere:

https://simple-regex.com/build

An Example

Regular Expressions don't have to be bulky? - No, they don't! Just have a look at this:

begin with any of (digit, letter, one of "._%+-") once or more,
literally "@",
any of (digit, letter, one of ".-") once or more,
literally ".",
letter at least 2,
must end, case insensitive

Or, if you like, a implementation in code itself:

$query = SRL::startsWith()
    ->anyOf(function (Builder $query) {
        $query->digit()
            ->letter()
            ->oneOf('._%+-');
    })->onceOrMore()
    ->literally('@')
    ->anyOf(function (Builder $query) {
        $query->digit()
            ->letter()
            ->oneOf('.-');
    })->onceOrMore()
    ->literally('.')
    ->letter()->atLeast(2)
    ->mustEnd()->caseInsensitive();

Yes, indeed, both examples are definitely longer than the corresponding regular expression:

/^([A-Z0-9._%+-])+@[A-Z0-9.-]+\.[A-Z]{2,}$/i

But, however, the above is quite better to read and definitely better to maintain, isn't it? And to top that off: It's much harder to forget to escape something like a dot in SRL.

Let's go through it real quick:

  1. First, we require the matching string to start. This way, we make sure the match won't begin in the middle of something.
  2. Now, we're matching either a digit, a letter, or one of the literal characters ., _, %, + or -. We expect there to be one or more of them.
  3. We now expect exactly one @ - Looks like an email address.
  4. Again, either digits, letters or . or -, once or multiple times.
  5. A dot. Seems to be the end of the TLDs name
  6. To the end, we'll expect two or more letters, for the TLD.
  7. We require the string to end now, to avoid matching stuff like [email protected].
  8. And of course, all of that should be case insensitive, since it's an email address.

Features

Using the Language

Above you can see two examples. The first one uses the language itself, the second one the Query Builder. Since using a language is more fluent than a builder, we wanted to make things as easy as possible for you.

$srl = new SRL('literally "colo", optional "u", literally "r"');
preg_match($srl, 'color') // 1
$srl->isMatching('colour') // true
$srl->isMatching('soup') // false

Everything below applies to both, the SRL itself and the Query Builder.

Matching

SRL is as simple as the example above states. To retrieve the built Regular Expression which can be used by external tools like preg_match, either use the ->get() method, or just let it cast to a string:

preg_match($query, '[email protected]');

Of course, you may use the builtin match methods for an even easier approach:

$query->isMatching('[email protected]'); // true
$query->isMatching('invalid-email.com'); // false

Capture Groups

Since regular expressions aren't only used for validation, capturing groups is supported by SRL as well. After defining the Regular Expression just like before, simply add a capture-group which will match the query defined in the lambda function. Optionally, a name for that capture group (color) can be set as well:

// Using SRL
$regEx = new SRL('literally "color:", whitespace, capture (letter once or more) as "color", literally "."');

// Using the query builder
$regEx = SRL::literally('color:')->whitespace()->capture(function (Builder $query) {
    $query->letter()->onceOrMore();
}, 'color')->literally('.');

$matches = $regEx->getMatches('Favorite color: green. Another color: yellow.');

echo $matches[0]->get('color'); // green
echo $matches[1]->get('color'); // yellow

Each match will be passed to a SRL\Match object, which will return the matches found.

Additional PCRE functions

Feel free to use all the available PCRE PHP functions in combination with SRL. Although, why bother? We've got wrappers for all common functions with additional features. Just like above, just apply one of the following methods directly on the SRL or Builder:

  • isMatching() - Validate if the expression matches the given string.
  • getMatches() - Get all matches for supplied capture groups.
  • getMatch() - Get first match for supplied capture groups.
  • replace() - Replace data using the expression.
  • split() - Split string into array through expression.
  • filter() - Filter items using the expression.

Lookarounds

In case you want some regular expressions to only apply in certain conditions, lookarounds are probably what you're searching for.

With queries like:

// SRL:
new SRL('capture (literally "foo") if followed by (literally "bar")');

// Query Builder:
SRL::capture(function (Builder $query) {
    $query->literally('foo');
})->ifFollowedBy(function (Builder $query) {
    $query->literally('bar');
});

you can easily capture 'foo', but only if this match is followed by 'bar'.

But to be honest, the Query Builder version is quite much code for such a simple thing, right? No problem! Not only are we supporting anonymous functions for sub-expressions, strings and Builder objects are supported as well. Isn't that great? Just have a look at one possible example:

SRL::capture('foo')->ifFollowedBy(SRL::literally('bar'));

If desired, lookbehinds are possible as well. Using ifAlreadyHad() you can validate a certain condition only if the previous string contained a specific pattern.

Performance

The built Regular Expression will be cached, so you don't have to worry about it being created every time you call the match-method. And, since it's a normal Regular Expression under the hood, performance won't be an issue.

Of course, building the expression may take some time, but in real life applications this shouldn't be noticeable. But if you like, you can build the expression somewhere else and just use the result in your app. If you do that, please keep the code for that query somewhere and link to it, otherwise the Regular Expression will be unreadable just as before.

Usage

Add the package to your require section in the composer.json-file and update your project.

"require": {
    "simpleregex/srl-php": "0.1.x-dev"
}
composer update

Things to do

We're definitely not done yet. There's much to come. A short list of stuff that's planned would contain:

  • More functionality
  • More documentation
  • Variable support
  • Rule the world

License

SRL is published under the MIT license. See LICENSE for more information.

Contribution

Like this project? Want to contribute? Awesome! Feel free to open some pull requests or just open an issue.

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