php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32470 preg_compile() Need way to pre-compile regex
Submitted: 2005-03-28 10:04 UTC Modified: 2005-04-12 20:30 UTC
From: sam_bravard at yahoo dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.0.3 OS: all
Private report: No CVE-ID: None
 [2005-03-28 10:04 UTC] sam_bravard at yahoo dot com
Description:
------------
PHP is missing the ability to pre-compile regex expressions and then use the pre-compiled regex.

This is a _major_ performance issue (100x) when processing files or text streams with regex's.  In PHP you have to recompile the regex for each line you process... a major waste of cpu time.

See Perl, .NET or Java's regex support for an example of how to use precompiled regex's.

Perhaps PHP can add something like the following and just overload the first argument to preg_match and friends:

$precompiled_expression = preg_compile("regex expression");
preg_match($precompiled_expression, $sourcedata, $matches);



Reproduce code:
---------------
$precompiled_expression = preg_compile("regex expression");
preg_match($precompiled_expression, $sourcedata, $matches);

Actual result:
--------------
Function doesn't exist... sorely needed for performance.

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-28 10:09 UTC] tony2001@php.net
All pcre_* funcs do not compile expressions each time, they use cache of precompiled regexps.
See pcre_get_compiled_regex_ex() in ext/pcre/php_pcre.c
Or you're proposing something different?
 [2005-04-05 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2005-04-12 20:30 UTC] sam_bravard at yahoo dot com
Terrific!  That's a fine solution.  I assumed the worst from the PHP docs.  

Coming from C#/Java mentality it's assumed you have to explicity compile for performance.

Perhaps someone on the doc team could add a short note mentioning the regex cache for those of us with blinders on ;-)

Thanks!
 [2016-09-10 08:35 UTC] alex at alex-at dot ru
While old and rusty, I'm returning this thread up.
PHP regexp cache seems to be per-thread, and that's sometimes very inconvenient. 

Imagine like 100 RPS doing ~10 different regexes each time. If these could be precompiled and cached between threads, it all would be fine. But if it's per-thread, then it's like ~1000 compilations each second.

Maybe there can be a way to store compiled regexps inside the opcache cache?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Aug 13 16:01:28 2024 UTC