🌐
PHP
php.net › manual › en › function.preg-match.php
PHP: preg_match - Manual
Here's a regex to validate against the schema for common MySQL identifiers: <?php $string = "$table_name"; if (preg_match("/[^\\d\\sa-zA-Z$_]/", $string)) echo "Failed validation"; ?>
🌐
W3Schools
w3schools.com › php › php_regex.asp
PHP Regular Expressions
flush() ob_clean() ob_end_clean() ob_end_flush() ob_flush() ob_get_clean() ob_get_contents() ob_get_flush() ob_get_length() ob_get_level() ob_gzhandler() ob_implicit_flush() ob_list_handlers() ob_start() output_add_rewrite_var() output_reset_rewrite_vars() PHP RegEx
Discussions

php - Documentation for ?: in regex? - Stack Overflow
A while ago, I saw in regex (at least in PHP) you can make a capturing group not capture by prepending ?:. Example $str = 'big blue ball'; $regex = '/b(ig|all)/'; preg_match_all($regex, $str, $ma... More on stackoverflow.com
🌐 stackoverflow.com
php - How can I validate regex? - Stack Overflow
I'd like to test the validity of a regular expression in PHP, preferably before it's used. Is the only way to do this actually trying a preg_match() and seeing if it returns FALSE? Is there a simp... More on stackoverflow.com
🌐 stackoverflow.com
php regex or | operator - Stack Overflow
I am doing a preg_replace: $pattern = '/ /'; $replacement = ""; $string = "hello how are you doing "; echo preg_replace($pattern, $replacement, $string); That will r... More on stackoverflow.com
🌐 stackoverflow.com
PHP regular expression
Find answers to PHP regular expression from the expert community at Experts Exchange More on experts-exchange.com
🌐 experts-exchange.com
November 17, 2014
🌐
Phpliveregex
phpliveregex.com
PHP Live Regex
Test PHP regular expressions live in your browser and generate sample code for preg_match, preg_match_all, preg_replace, preg_grep, and preg_split!
🌐
Rexegg
rexegg.com › regex-php.php
PHP Regex Tutorial
PHP Regex Tutorial. Discusses Preg functions, PCRE, code examples.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-regular-expressions
PHP | Regular Expressions - GeeksforGeeks
July 12, 2025 - The exact sequence of characters ... on a pattern definition. Regular Expression is a compact way of describing a string pattern that matches a particular amount of text. As you know, PHP is an open-source language ...
🌐
PHP.Watch
php.watch › articles › php-regex-readability
Writing better Regular Expressions in PHP • PHP.Watch
May 26, 2021 - Syntax of a non-capturing group is a brace that starts with (?:, and ends with ). Regex engine asserts the expression inside the braces, but it is not returned as a match; i.e.
🌐
Regex101
regex101.com
regex101: build, test, and debug regex
Flavor: PCRE2 (PHP) Match · Substitution · List · Unit Tests · PipelinePro · Code Generator · Regex Debugger · Export Matches · Benchmark RegexPro · Format RegexPro · S⁠ponsors · digidib · when security, quality, and reliability matter · An explanation of your regex will be automatically generated as you type.
Find elsewhere
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals › regular expressions
Search and Replace Strings With Regular Expressions in PHP | Envato Tuts+
March 30, 2021 - In this tutorial, we learned how to use some common and useful functions related to regular expressions in PHP. We began by learning how to use preg_match() or preg_match_all() to get all the strings that match a specific pattern.
🌐
YouTube
youtube.com › watch
PHP Regular Expressions Tutorial - YouTube
Upgrade your Clever Techie learning experience:https://www.patreon.com/clevertechieUPDATE! (9/13/19) New features and improvements for Clever Techie Patreons...
Published: November 27, 2016
🌐
Jotform
jotform.com › blog › advice › getting started with php regular expressions
Getting Started with PHP Regular Expressions | Jotform Blog
June 25, 2024 - In PHP we have a total of nine PCRE functions which we can use. Here’s the list: preg_filter – performs a regular expression search and replace · preg_grep – returns array entries that match a pattern · preg_last_error – returns the error code of the last PCRE regex execution
Top answer
1 of 6
35

It's available on the Subpatterns page of the official documentation.

The fact that plain parentheses fulfill two functions is not always helpful. There are often times when a grouping subpattern is required without a capturing requirement. If an opening parenthesis is followed by "?:", the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns. For example, if the string "the white queen" is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are "white queen" and "queen", and are numbered 1 and 2. The maximum number of captured substrings is 99, and the maximum number of all subpatterns, both capturing and non-capturing, is 200.

It's also good to note that you can set options for the subpattern with it. For example, if you want only the sub-pattern to be case insensitive, you can do:

(?i:foo)bar

Will match:

  • foobar
  • Foobar
  • FoObar
  • ...etc

But not

  • fooBar
  • FooBAR
  • ...etc

Oh, and while the official documentation doesn't actually explicitly name the syntax, it does refer to it later on as a "non-capturing subpattern" (which makes complete sense, and is what I would call it anyway, since it's not really a "group", but a subpattern)...

2 of 6
8

(?:) as a whole represents a non-capturing group.

Regular-expressions.info mentions this syntax :

The question mark and the colon after the opening round bracket are the special syntax that you can use to tell the regex engine that this pair of brackets should not create a backreference. Note the question mark [...] is the regex operator that makes the previous token optional. This operator cannot appear after an opening round bracket, because an opening bracket by itself is not a valid regex token. Therefore, there is no confusion between the question mark as an operator to make a token optional, and the question mark as a character to change the properties of a pair of round brackets. The colon indicates that the change we want to make is to turn off capturing the backreference.

🌐
Medium
medium.com › @TechnologyDiaries › php-regular-expressions-regex-a-complete-beginners-guide-8fc93cf2c68a
PHP Regular Expressions (Regex): A Complete Beginner’s Guide | by Technology Diaries | Medium
August 18, 2025 - Regular expressions are sequences of characters that define search patterns. Think of them as a sophisticated “find and replace” tool that can handle complex text operations with just a few lines of code. In PHP, regex is primarily used through PCRE (Perl Compatible Regular Expressions) functions like preg_match(), preg_replace(), and preg_split()....
🌐
Regexbuddy
regexbuddy.com › php.html
PHP ereg and preg Regex Functions
Since PHP 7.3.0 they are based on PCRE2. The ereg functions implement POSIX regular expressions which offer a limited syntax. The ereg functions are deprecated, but RegexBuddy still fully supports them so you can maintain existing code or convert your regular expressions from PHP’s ereg flavor to the preg flavor.
Top answer
1 of 12
160
// This is valid, both opening ( and closing )
var_dump(preg_match('~Valid(Regular)Expression~', '') === false);
// This is invalid, no opening ( for the closing )
var_dump(preg_match('~InvalidRegular)Expression~', '') === false);

As the user pozs said, also consider putting @ in front of preg_match() (@preg_match()) in a testing environment to prevent warnings or notices.

To validate a RegExp just run it against null (no need to know the data you want to test against upfront). If it returns explicit false (=== false), it's broken. Otherwise it's valid though it need not match anything.

So there's no need to write your own RegExp validator. It's wasted time...

2 of 12
27

I created a simple function that can be called to checking preg

function is_preg_error()
{
    $errors = array(
        PREG_NO_ERROR               => 'Code 0 : No errors',
        PREG_INTERNAL_ERROR         => 'Code 1 : There was an internal PCRE error',
        PREG_BACKTRACK_LIMIT_ERROR  => 'Code 2 : Backtrack limit was exhausted',
        PREG_RECURSION_LIMIT_ERROR  => 'Code 3 : Recursion limit was exhausted',
        PREG_BAD_UTF8_ERROR         => 'Code 4 : The offset didn\'t correspond to the begin of a valid UTF-8 code point',
        PREG_BAD_UTF8_OFFSET_ERROR  => 'Code 5 : Malformed UTF-8 data',
    );

    return $errors[preg_last_error()];
}

You can call this function using the follow code :

preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
echo is_preg_error();

Alternative - Regular Expression Online Tester

  • RegExr
  • PHP Regular Expression Tester
  • Regular Expression Tool
🌐
Reintech
reintech.io › blog › php-and-regular-expressions-pattern-matching-with-preg-functions
PHP and Regular Expressions: Pattern Matching with preg_* Functions
April 28, 2023 - Escapes special regex characters in a string, crucial when incorporating user input into patterns. The preg_match() function tests whether a pattern exists in a string. It's your go-to function for validation tasks and conditional logic based on pattern presence. $userInput = "I love PHP programming!"; $pattern = "/PHP/"; if (preg_match($pattern, $userInput)) { echo "Found PHP reference"; } else { echo "No PHP mentioned"; } // Output: Found PHP reference
🌐
CatsWhoCode
catswhocode.com › development & programming › php regex for web developers
PHP Regex for Web Developers
January 9, 2025 - A regular expression (regex or regexp for short) is a special text string for describing a search pattern. A regex pattern matches a target string. The following table describes most common regex: PHP has many useful functions to work with regular ...
🌐
Experts Exchange
experts-exchange.com › questions › 28564006 › PHP-regular-expression.html
Solved: PHP regular expression | Experts Exchange
November 17, 2014 - '345' , '*' , '*,123,*,456' , '*,a,*' , '*,123,' ) ; // RUN THE TEST CASES foreach ($tests as $test) { echo PHP_EOL . htmlentities($test); if (valid_substrings($test)) echo ' VALID'; else echo ' INVALID'; } // A FUNCTION TO TEST THE SUBSTRINGS function valid_substrings($str) { // A REGULAR EXPRESSION TO DISALLOW ANYTHING BUT NUMBERS OR THE ASTERISK $rgx = '#' // REGEX DELIMITER .