W3Schools
w3schools.com › php › php_regex.asp
PHP Regular Expressions
Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions
Regex101
regex101.com
regex101: build, test, and debug regex
Online regex tester and debugger with real-time match highlighting, detailed explanations, substitutions, unit tests, benchmarking, and code generation. Supports PCRE2, JavaScript, Python, Go, Java, .NET, Rust, POSIX ERE, and POSIX BRE.
Regular expression pattern works on regex101.com but not in php CLI
Honestly I would't use regex for this. I would write a script that parses the file line by line and check if a line starts with a timestamp. Then use a smaller simpler regex to extract the timestamps and you can parse the next few lines that contain the text in total until you encounter a new timestamp or number block. More on reddit.com
Easy work with regex groups;
That sort of reminds me of applicative parser combinators, although less composable. There are few parser combinator libraries for PHP but they aim for fully monadic parsers, which will probably never reach the performance of an optimized regular expression engine. I wonder if we could make a library with nice composable interface that would build a single regular expression in the background (imagining something like Nikita Popov’s FastRoute ), followed by a thin hydration layer. For example how this can be achieved in other languages, in Haskell, I can define a data structure and then “regular expression objects” (RE parsers) that will produce the final datastructure with the help of smaller parsers: import Text.Regex.Applicative data TinWoodman = TinWoodman { head :: String, arms :: String, legs :: String, body :: String, heart :: Maybe String } deriving (Show) anyStringRegex :: RE Char String anyStringRegex = many anySym tinWoodmanRegex :: RE Char TinWoodman tinWoodmanRegex = TinWoodman <$> (anyStringRegex *> string "head") <*> (anyStringRegex *> string "arms") <*> (anyStringRegex *> string "legs") <*> (anyStringRegex *> string "body") <*> (optional (anyStringRegex *> string "heart")) <* anyStringRegex In the example above, something like the following happens: string function creates a “regex object” that accepts the string given to it and also returns it as the result. anyStringRegex is .*. <*> and <$> help to pass the results of parser on the right hand side to the TinWoodman constructor function. optional function takes a parser and returns a new parser that will return the result of the inner parser wrapped in Just, or Nothing if the inner parser does not match. *> runs the parser on the left hand side, discards its result, and then runs the parser on the right and returns its result. The parser can be executed as follows: ghci> match tinWoodmanRegex "One of the big trees had been partly chopped through, and standing beside it, with an uplifted axe in his hands, was a man made entirely of tin. His head and arms and legs were jointed upon his body, but he stood perfectly motionless, as if he could not stir at all." Just (TinWoodman {head = "head", arms = "arms", legs = "legs", body = "body", heart = Nothing}) More on reddit.com
[PHP] I made regex on regex101.com, but it doesnt work in my code.
Click on the Code Generator button https://regex101.com/r/8xZ0a5/1/codegen?language=php The issue is the number of backslashes in your pattern. Because it's quoted (a "string") you need \\ to match a single \ - so you need 4 in your pattern not 2. More on reddit.com
Online regex tester and debugger: JavaScript, Python, PHP, and PCRE
I was ready to recommend Regexr over this, the explanation panels are actually really cool and useful. More on reddit.com
03:33
PHP Regular Expressions: Regex Basics for Beginners - YouTube
Regex Basics Ep 1: What Are Regular Expressions? (Regex101 Tutorial) ...
09:34
PHP Regular Expressions (Regex) Tutorial: Advanced Pattern Matching ...
18:09
Learn Regex Online: Basic Tryouts with Regex101 for Beginners - ...
08:34
59: Functions Using Regular Expressions | PHP Tutorial | Learn ...
14:50
PHP Regular Expressions Tutorial - YouTube
GeeksforGeeks
geeksforgeeks.org › php › php-regular-expressions
PHP | Regular Expressions - GeeksforGeeks
July 12, 2025 - The exact sequence of characters are unpredictable beforehand, so the regex helps in fetching the required strings based 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 commonly used for website creation, it provides regular expression functions as an important tool.
Phpliveregex
phpliveregex.com
PHP Live Regex
i case insensitive m treat as multi-line string s dot matches newline x ignore whitespace in regex A matches only at the start of string D matches only at the end of string U non-greedy matching by default
Rexegg
rexegg.com › regex-php.php
PHP Regex Tutorial
Hit Ctrl + F to search for "preg_regex_to_pattern", "preg_pattern_error" and "preg_regex_error". If your version of PHP is 5.2.4 or later (phpinfo is your friend), you can use a wonderful PCRE escape sequence: \K. In the middle of a pattern, \K says "reset the beginning of the reported match to this point". Anything that was matched before the \K goes unreported, a bit like in a lookbehind. For example, on the string "Marlon Brando", the pattern (?i)marlon \Kbrando will return "Brando".
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
Regular expression tester with syntax highlighting, PHP / PCRE & JS Support, contextual help, cheat sheet, reference, and searchable community patterns.
Reddit
reddit.com › r/phphelp › regular expression pattern works on regex101.com but not in php cli
r/PHPhelp on Reddit: Regular expression pattern works on regex101.com but not in php CLI
January 15, 2023 -
Im trying to extract subtitle entries out of a SRT file. Ive written a script that opens the file and with the help of regex fetches the strings. Ive testes the regex pattern on regex101.com using the correct php engine. And it works. For some reason php CLI has an issue with the pattern. It seems that newlines "\n" in the pattern is either causing the problem or the imported file strips all newlines.
here is the regex pattern
^(?<NR>\d+)\n(?<START>\d\d:\d\d:\d\d,\d\d\d)(?>\s?-->\s?)(?<END>\d\d:\d\d:\d\d,\d\d\d)\n(?<STRING>(?>.+\n?)+)
Dummy text:
1 00:00:46,296 --> 00:00:50,425 They were startled and frightened, thinking they saw a ghost. 2 00:00:50,467 --> 00:00:52,386 He said to them, 3 00:00:54,179 --> 00:00:57,140 "Why are you troubled, and why do doubts rise in your minds? 4 00:00:57,182 --> 00:00:59,977 <i>Look at my hands and my feet. It is I myself!</i>
PHP code
<?php $path1 = $argv[1]; $file1_content = file_get_contents($path1); $pattern = '%^(?<NR>\d+)\n(?<START>\d\d:\d\d:\d\d,\d\d\d)(?>\s?-->\s?)(?<END>\d\d:\d\d:\d\d,\d\d\d)\n(?<STRING>(?>.+\n?)+)%'; $success = preg_match_all($pattern, $file1_content, $matches); var_dump($matches);
CLI Instructions
-
open a text file and save the dummy content into it.
-
Open your shell and goto dir of txt file.
-
type %php -f [php-script-file] -- [full-path-to-dummy-file]
Top answer 1 of 2
5
Honestly I would't use regex for this. I would write a script that parses the file line by line and check if a line starts with a timestamp. Then use a smaller simpler regex to extract the timestamps and you can parse the next few lines that contain the text in total until you encounter a new timestamp or number block.
2 of 2
1
Will work if you change your regular expression to the following: $pattern = "/^(?\d+)\n(?\d\d:\d\d:\d\d,\d\d\d)(?>\s?-->\s?)(?\d\d:\d\d:\d\d,\d\d\d)\n(?(?>.+\n?)+)/m"; And change your preg_match_all function call to: $success = preg_match_all($pattern, $file1_content, $matches, PREG_SET_ORDER); Note: Instead of typing \d\d\d, you can type \d{3}. You could use this format to simplify your expression.
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-regular-expressions.php
Regular Expressions in PHP - Tutorial Republic
<?php $pattern = "/^color/im"; $text = "Color red is more visible than \ncolor blue in daylight."; $matches = preg_match_all($pattern, $text, $array); echo $matches . " matches were found."; ?> A word boundary character ( \b) helps you search for the words that begins and/or ends with a pattern. For example, the regexp /\bcar/ matches the words beginning with the pattern car, and would match cart, carrot, or cartoon, but would not match oscar.
ZetCode
zetcode.com › php › regex
Regular expressions in PHP
January 10, 2023 - $ php extract_matches.php The 10:10:22 is split into: Hour: 10 Minute: 10 Second: 22 The 23:23:11 is split into: Hour: 23 Minute: 23 Second: 11 The 09:06:56 is split into: Hour: 09 Minute: 06 Second: 56 · Next have a practical example. We create a regex pattern for checking email addresses.
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 - Whether you’re validating user input, searching for specific patterns, or replacing text, regex can make your code more efficient and robust. 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().
Tutorialspoint
tutorialspoint.com › php › php_regular_expression.htm
PHP - Regular Expressions
Regular expressions are nothing more than a sequence or pattern of characters itself. They provide the foundation for pattern-matching functionality. Using regular expression you can search a particular string inside a another string, you can