Re: [DISCUSSION] Validating regex pattern
Hi
Am 2025-10-02 13:03, schrieb Ayesh Karunaratne:
I completely agree with this and the previous reply from Juliette.
I also fully agree with Juliette: This is something that needs to be done in ext/pcre.
A PREG_THROW_ON_ERROR flag would be the best of both worlds:
- Provides a nice safe-guard against potentially invalid regexps and
surfaces them up as an exception.
- Provides validation functionality with engine-provided error messages.
- We do not need to keep up with the regex engines because validation
is done by the engine.
- Works similar to the JSON_THROW_ON_ERROR flag.
I however don't think that PREG_THROW_ON_ERROR is the best solution here. A flag that effectively *always* needs to be passed is not good API design and functions like preg_replace() don't currently support flags. While it would definitely increase the scope of an RFC, I second Thomas' notion of providing a new (object-oriented) pcre API that would also allow for passing modifiers independently of the expression itself, avoiding the need for delimiters (and escaping them when dynamically constructing a regex).
Best regards
Tim Düsterhus
Thread (11 messages)