Re: Pre-RFC: adding a Regex\CompiledRegex class for compiling regular expressions
| From: | Gina P. Banyard | Date: | Fri, 25 Sep 2026 10:43:01 +0000 |
| Subject: | Re: Pre-RFC: adding a Regex\CompiledRegex class for compiling regular expressions | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132632@lists.php.net to get a copy of this message | ||
On Friday, 25 September 2026 at 07:59, Sjoerd Langkemper <sjoerd-php@linuxonly.nl> wrote:
> On Wed, Sep 23, 2026, at 21:01, Gina P. Banyard wrote:
>
>> This class could lay the foundation on which to build a new nice OO API
>
> Nice. I am in favor of improving the API, instead of slapping more flags onto the existing one.
>
>> the pattern *must* be a UTF-8 pattern
>
> Does this also apply to the subject? I recently had a reason to remove the u modifier from a
> regex: if the input is not valid UTF8, preg_match silently fails without a match. I think I was
> scraping web pages and discovered that some webpages don't contain valid UTF8, and that made it
> impossible to use preg_match with UTF8 on it.
It is possible to set the PCRE2_MATCH_INVALID_UTF flag which allows to match part of an invalid
UTF-8 subject, details of this are described at the end of https://www.pcre.org/current/doc/html/pcre2unicode.html
in the "MATCHING IN INVALID UTF STRINGS" section.
However it does seem to have some limitations and one may indeed need to offer a way to disable
UTF-8 mode altogether.
>> returning a bool type
>
> Would it make more sense to return a Match object, with captured groups?
How would this Match object work? What is it's API? And if you don't care about captured
groups why would you need to instantiate an (or multiple) object when a boolean value would do just
fine.
That's why I said I don't want to spend time on designing an API as this frankly needs
multiple methods and is going to be complicated.
There are also new PCRE2 features not exposed to userland where it may make sense to do so in a
greenfield API.
> The proposed API would result in calls like this:
> new CompiledRegex(".*", false, false, false, true, true, true, false, true)
> where it is hard to determine what all the true/false parameters mean. Would it be better to
> pass enums instead? Or is this solved by editor hints nowadays
Arguably we have named parameters to just toggle the options that are different from the default, so
I wouldn't be writing a call to it like this nowadays anyway.
But Nora did suggest an array of enum RegexOptions on the PR, and my reply was that if PHP had a way
to pass an Enum set this would be the best approach as you'd only have one parameter.
In any case I don't have strong feelings about this part (or maybe I should spend some time and
figure out a way to make enum sets a reality before)
Best regards,
Gina P. Banyard