Skip to content

Releases: MaestroError/eloquent-regex

1.4.0

28 Dec 15:22
Compare
Choose a tag to compare

Added new useful features (action methods), support for named groups, some small fixes and updated docs. For details, please refer to Full Changelog at bottom of this release note, but before that, let's discuss the main features added.

Replace

Replaces found matches in the given source string using the provided callback. For example:

EloquentRegex::start("This is a #test to wrap hashtags in #anchor tag")
    ->hash()->text()
    ->replace(function($foundItem) {
        return "<a href='$foundItem'>" . $foundItem . "</a>";
    });

Will return "This is a <a href='#test'>#test</a> to wrap hashtags in <a href='#anchor'>#anchor</a> tag".
Of course, you can apply any functions inside the callback and tweak your new string as you need 👍

Check the new section in docs for more information.

Search & Search Reverse

Searches for the keyword or pattern (including ready-to-use patterns too) in multiline text and returns lines where the subject is found. The searchReverse does the same, but returns lines where subject is not found.

For example, let's filter out only those lines where any email is included using search method:

EloquentRegex::source(
    "
    Please contact us via email at info@example.com for more details.
    For support inquiries, you can also email us at support@example.com.
    Our marketing team is reachable at marketing@example.com for collaborations.
    For urgent matters, you can reach out through the phone number provided.
    Subscribe to our newsletter to stay updated with the latest news.
    Feel free to send feedback directly to our office address.
    Any emails sent after 5 PM may be responded to the next business day.
    Check the FAQ section for answers to common questions.
    Social media channels are also available for quick updates.
    We value your input and encourage you to share your thoughts.
    "
    )
    ->search(function ($pattern) {
        $pattern->email();
    });

It will return array/collection (in case of using inside laravel):

[
    'Please contact us via email at info@example.com for more details.',
    'For support inquiries, you can also email us at support@example.com.',
    'Our marketing team is reachable at marketing@example.com for collaborations.'
]

Let's check the searchReverse usage example in case when we need to review all logs except the "INFO" type (but using keyword instead of pattern this time):

EloquentRegex::source(
    "
    [2024-12-23 10:00:00] INFO: User logged in.\n
    [2024-12-25 10:05:00] ERROR: Unable to connect to database.\n
    [2024-12-25 10:10:00] INFO: User updated profile.\n
    [2024-12-15 10:15:00] WARNING: Disk space running low.\n
    [2024-12-34 10:20:00] ERROR: Timeout while fetching data.\n
    "
    )
    ->searchReverse("INFO");

Only the lines that don't contain "INFO" will be returned:

[
    '[2024-12-25 10:05:00] ERROR: Unable to connect to database.',
    '[2024-12-15 10:15:00] WARNING: Disk space running low.',
    '[2024-12-34 10:20:00] ERROR: Timeout while fetching data.',
]

As simple as that!
These methods are especially useful while processing large files like logs or JSON. And the search method has great performance, check the benchmark results here.

Swap

Allows you to swap any kind of data logically in every match found and returns the whole new array of strings you need. For example, you can build new URIs from the old ones:

$builder= EloquentRegex::start("URIs: /container-tbilisi-1585, /container-berlin-1234, /container-tbilisi-2555")
    ->slash() // "/"
    ->exact("container") // "container" (static part of URI)
    ->dash() // "-"
    ->namedGroup(function ($pattern) {
        return $pattern->text();
    }, "City") // Text between dashes, Grouped & named as "city"
    ->dash() // "-"
    ->namedGroup(function ($pattern) {
        return $pattern->digitsRange(2, 5);
    }, "id"); // Numbers at end, Grouped & named as "id"

// Using swap with pattern string
// which will swap placeholders like "[ID]" with
// Extracted data for each found match
$builder->swap("/container/[ID]?city=[CITY]");

Will return ready-to-use array of the new URIs:

[
    '/container/1585?city=tbilisi',
    '/container/1234?city=berlin',
    '/container/2555?city=tbilisi'
]

This method utilizes the new namedGroup method which actually is a regex feature and its support comes with this release, check the groups section in docs

In the previous example we used so called "pattern string" with [VAR] placeholders, lets check the example of the same swap method used via callback. In this case, we will process natural language to find the Jira's issue codes and get separate notification for each:

$builder = EloquentRegex::start("Issues in progress: RI-2142, RI-1234, PO-2555");
$builder
    ->namedGroup(function ($pattern) {
        return $pattern->textUppercase(2);
    }, "project", 1) // 2 uppercase char named as "project"
    ->dash() // "-"
    ->namedGroup(function ($pattern) {
        return $pattern->digitsRange(2, 4);
    }, "issue", 1) // from 2 to 4 digits named as "issue"
    ->end();

    $results = $result->swap(function ($data) {
        return "The issue #" . $data["issue"] . " of project " . $data["project"] ." is in progress";
    });

Will return the array of sentences we built via callback:

[
    'The issue #2142 of project RI is in progress',
    'The issue #1234 of project RI is in progress',
    'The issue #2555 of project PO is in progress'
]

Let me remind you that any updates mentioned above are also available now in SimplifiedRegex NPM package, you can check the release note here.

Full Changelog: 1.3.1...1.4.0

Happy new coding year! 🎄

Fixed return type for BuilderPattern's `get` method

09 May 14:34
Compare
Choose a tag to compare

Updated URL and Date patterns, updated docs and new GPT

09 Apr 17:17
Compare
Choose a tag to compare

Now the EloquentRegex comes with its own GPT assistant

Full Changelog: 1.2.0...1.3.0

Docs and support for laravel collection

04 Mar 19:26
Compare
Choose a tag to compare

Features:

  • Return collection on get method if laravel is available.✔️
  • Full documentation.✔️
  • Return captured groups while using group() method with get().✔️

Fixes:

  • Remove the default quantifier inside charSet.✔️
  • Remove extra "[]" inside charSet.✔️
  • Rename the "allowChars" option to "onlyChars".✔️

What's Changed

Full Changelog: 1.0.1...1.2.0

Fix file name

28 Feb 15:50
Compare
Choose a tag to compare
1.0.1

Fix file name

Laravel facade, quantifiers update and basic documentation

28 Feb 15:44
Compare
Choose a tag to compare

What's Changed

Full Changelog: 0.2.0...1.0.0

Static wrapper and tests on PR

27 Feb 13:27
Compare
Choose a tag to compare

Created static wrapper to make available static use for every user

What's Changed

New Contributors

Full Changelog: 0.1.0...0.2.0

Initial release

26 Feb 18:24
Compare
Choose a tag to compare

The great starting point, all essential features are ready:

  • Pattern builder for custom pattern creating (BuilderPattern)
  • Ready-to-use Patterns for common use cases
  • Options for extra assertions