9 min read

How Eloquent Regex Package Came About?

Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.

One of our talented developers, Revaz, has recently created a package, Eloquent Regex, that simplifies and improves working with regular expressions without sacrificing its powerful features. Check out this blog, where Revaz guides us through the package, shares its inspiration, and reveals how the solution came about.

Introduction

Have you ever found yourself avoiding Regular Expressions (Regex) at all costs, even if it meant investing extra coding time? You’re not alone. That’s quite common among developers due to its complicated syntax and the time-consuming process of creating and debugging regex patterns.

Even though it has lots of powerful features, many developers still choose less efficient methods to avoid its complexities. I’ve been in that boat, too, so I know that hurdle. 

This is why I decided to create a solution that would enable developers to enjoy Regex’s great functionalities without constant testing and debugging. That’s how I developed the Eloquent Regex package, a tool designed to simplify Regex and make it more accessible and user-friendly for everyday coding tasks.

With a straightforward interface, Eloquent Regex simplifies the process of building and executing Regex patterns in PHP applications.

Plus, the package offers a neat feature: if you’re subscribed to ChatGPT Plus, you can use EloquentRegex easily with the help of the EloquentRegex Assistant GPT. I’ve equipped this custom GPT with all the necessary information about documentation, examples and use cases. This means it can guide you through using the Eloquent regex package without even needing to read its documentation. Just ask the GPT anything about the package, and you’ll receive detailed assistance on how to implement it, as well as other related matters.

The Inspiration Behind Creating Eloquent Regex

When I was mulling over the idea of this package, I wondered if there were any platforms or software out there with similar principles that could simplify my development process. I also wanted to ensure that my solution would be easy to understand and use for the Laravel community. 

That’s when I remembered Laravel Eloquent object-relational mapper (ORM), which provides a beautiful, simple implementation for working with your database. Each database table has a corresponding “Model” that is used to interact with that table. But in this case, the database table itself is just a source, right? So, I thought, why couldn’t the regex’s target string be a source too?

Realizing that Laravel Eloquent ORM could serve as the core principle for building my new package, I designed it to simplify regex syntax in the same way that Eloquent ORM simplifies MySQL syntax. This is the main inspiration and backbone of Eloquent Regex.

EloquentRegex::source('test@example.com')->email()->check(); // True

Implementing Eloquent Regex will be especially useful and straightforward for Laravel developers who are already familiar with Eloquent ORM syntax. However, developers can implement this package in any PHP project.

Challenges Faced Along The Way

One of the main challenges I faced during the development process was simplifying the inherently complex syntax of regular expressions without losing their powerful features. Most of the developers, including myself, have probably had difficulty memorizing and debugging complicated regex patterns. This can quickly become a big headache, especially for those who are less familiar with regex intricacies.

Another significant challenge was ensuring the tool was versatile across different programming environments and use cases. Regular expressions are a fundamental part of many coding projects, from simple string validations to complex text-processing tasks. So, creating a solution that could adapt to such a wide array of needs while maintaining a simple and intuitive interface required careful planning and innovative thinking.

Probably one of the biggest obstacles I faced in creating the Eloquent Regex package was figuring out how to implement the BuilderPattern. My goal was to make it easy for developers to create any regex pattern they needed using straightforward methods that resemble an assembler for regex patterns. However, this goal brought along a complexity beyond just providing a set of ready-to-use patterns.

The BuilderPattern was built to be the foundation of Eloquent Regex, allowing developers to build regex patterns from left to right. This required careful planning to include all the different regex features like quantifiersgroupingslookaheads, and more. Achieving this without sacrificing the simplicity and elegance that Laravel developers, in particular, have come to expect was a challenge. The pattern needed to be intuitive enough for developers to construct complex regex expressions effortlessly yet powerful enough not to limit their capabilities.

During the implementation process, there were many rounds of iterations and refinements to get things just right. For example:

$result = EloquentRegex::start('ID123456')->literal('ID')->digitsRange(1, 10)->check();
// $result would be True from “ID1” to ”ID0123456789”

This code snippet demonstrates the capability of the BuilderPattern. It creates a pattern that matches a string starting with “ID” followed by any number of digits ranging from 1 to 10.

Another example illustrates the use of quantifiers:

// Matches strings with exactly 2 underscores
$result = EloquentRegex::start($yourString)->digits()->underscore('2')->digits()->check();
// Result would be true in cases $yourString: "1235__158", but "1235___158" and "1235_158" will be false

The challenges of developing the BuilderPattern were multifaceted, from ensuring a user-friendly interface to maintaining robustness and flexibility for all possible regex use cases. Each line of code was developed with the user’s experience in mind, aiming to transform the daunting task of creating regex patterns into a smooth, straightforward process.

The successful integration of the BuilderPattern into Eloquent Regex played a huge role in overcoming a significant challenge and setting a new standard for regex usability. In fact, it was the commitment that made regex accessible to all types of developers, regardless of their familiarity with regex syntax.

Solution – Eloquent Regex

Eloquent Regex was designed to address these challenges head-on. The package offers a fluent, expressive syntax for regular expressions that abstracts away much of their complexity. This not only makes regex more approachable for developers but also speeds up the development process by reducing the need for constant testing and debugging.

The versatility challenge was met by implementing a wide range of predefined patterns that cover the most common use cases, from email and URL validation to more complex patterns like credit card numbers and IP address formats. 

EloquentRegex::source("Visa: 4111 1111 1111 1111, MasterCard: 5500 0000 0000 0004, Amex: 3400 000000 00009")
->creditCardNumber("visa, amex")->get();
// Returns only Visa and Amex card numbers

Eloquent Regex offers the flexibility to create custom patterns using the same easy-to-understand syntax. This allows developers to address unique requirements without leaving the comfort of the Eloquent Regex environment.

Eloquent Regex uses Laravel’s existing features and practices, such as facades and service providers, to offer developers a smooth and intuitive experience. It fits naturally into the Laravel ecosystem, making it an easy pick for developers looking for a regex solution that feels like an extension of Laravel itself.

Possible Use Cases and Future Development Plans

Eloquent Regex opens doors to lots of potential use cases across various programming scenarios. Let’s take a look at some applications where Eloquent Regex can be really useful:

  • Form Validation: Easily check and ensure that user inputs in web forms are correct and accurate.
  • Data Extraction: Extract specific information from text, like emails or URLs, to simplify data processing.
  • Text Parsing: Parse and manipulate text for tasks like parsing log files, extracting keywords, or formatting text according to specific patterns.
  • Search Functionality: Improve search functionalities by implementing custom regex patterns to filter and retrieve relevant information from large datasets.
  • Input Sanitization: Protect your application from malicious inputs by sanitizing user inputs using regex patterns.
  • URL Routing: Create dynamic and expressive URL routing patterns for routing requests in web applications.
  • Custom Pattern Matching: Customize regex patterns to match specific requirements unique to your project.

My vision for the future extends beyond Eloquent Regex’s current scope. I’m committed to broadening its impact by making this simplified regex logic accessible across all programming languages, including JavaScript and Go. This way, developers from different backgrounds will be able to implement Eloquent Regex into their projects, breaking down barriers and making string manipulation easier for a much broader audience.

Meet the authors

We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.

Contact us