🌐
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

  1. open a text file and save the dummy content into it.

  2. Open your shell and goto dir of txt file.

  3. type %php -f [php-script-file] -- [full-path-to-dummy-file]

🌐
Reddit
reddit.com › r › engineerman › comments › bnrck3 › regex_101_online_regex_tester_and_debugger_php
r/engineerman - RegEx 101 - Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript
regex101.com/ Resource · 0 comments · share · save · hide · report · 100% Upvoted · Log in or sign up to leave a comment · Log InSign Up · Sort by: top (suggested) View discussions in 21 other communities · no comments yet · Be the first to share what you think!
🌐
Reddit
reddit.com › r/php › online regex tester and debugger: javascript, python, php, and pcre
r/PHP on Reddit: Online regex tester and debugger: JavaScript, Python, PHP, and PCRE
January 22, 2015 - If you’re looking for a clean and simple online regex tool, regex101 is definitely a popular choice. The breakdown explanations and flavour selection (JS, Python, PHP, PCRE) make debugging much easier, especially when you’re switching between languages.
🌐
Reddit
reddit.com › domain › regex101.com
regex101.com on reddit.com • r/regex101.com
RegEx 101 - Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScriptResource (regex101.com)
🌐
Reddit
reddit.com › r/regex › need help with regex101 problem
r/regex on Reddit: Need help with RegEx101 problem
July 21, 2019 -

Hey all, I'm working my way through the quizzes at RegEx101 and I'm currently stock on problem 10. After hours of hammering away at it, I don't think I'm going to be able to get it or I might just be misunderstanding the question. This is how it is worded:

"For every occurence of the char #, match the previous character and save it in a group (backreference). Example: for the text "a#bc# -#", set backreferences with a, c and -. You are not allowed to consume the hash character."

And my RegEx for this is /([^\s])+(?=#)/g which should be capturing anything that isn't a space, which occurs before a hash. When trying to submit the answer, it is failing with the response "Test 7/18: You are supposed to catch all occurrences!"

Does anyone have any hints or suggestions?

🌐
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.
🌐
Regex101
regex101.com › r › hkvPij › 1
regex101: PHP
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
🌐
Reddit
reddit.com › r/php › form data validation with regular expression
r/PHP on Reddit: Form data validation with regular expression
April 15, 2025 -

My form builder site allows users to specify a regular expression for html 5 input pattern validation.

In addition to validating this on the client side with html5, the service also validates on the server side after submission as client side validation can be circumvented (e.g. by removing the pattern attribute in browser dev tools).

Client side regex on pattern attribute is compiled with the "v" flag which "enhances Unicode support in regular expressions, enabling the use of set notation, string literals within character classes, and properties of strings".

On the server side my script checks the input matches the pattern but the "v" flag is not available in php regex functions (I'm on php 8.3) so I am using the "u" flag.

Is this likely to fail in any circumstance? Is there a way to ensure the results are the same in JS and PHP?

Thanks guys.

Find elsewhere
🌐
W3Schools
w3schools.com › php › php_regex.asp
PHP Regular Expressions
Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions
🌐
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 › regex101
r/regex101 - Reddit
May 20, 2022 - r/regex101: Place to discuss about regex101 (mainly the tasks). https://regex101.com/quiz/
🌐
Regex101
regex101.com › r › qP1rP4 › 1
regex101: PHP identifier
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
🌐
Reddit
old.reddit.com › r › PHP › comments › cz8ol › what_is_the_perfect_email_regex_to_use
What is the perfect email regex to use? : PHP
I can never seem to find or come up with a "perfect" regex to use. Some accept invalid emails, some reject emails with a + sign in them. What's a...
🌐
Reddit
reddit.com › r/regex › help with a regex 101 problem
Help with a regex 101 problem : r/regex
March 10, 2023 - 6.5K subscribers in the regex community. This is a friendly place to learn about or get help with regular expressions. Please read & understand the…
🌐
Regex101
regex101.com › r › bB9kC2 › 1
regex101: recognize php
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
🌐
Reddit
reddit.com › r/phphelp › help php regex pattern
r/PHPhelp on Reddit: Help PHP Regex Pattern
August 13, 2021 -

I am trying to remove all occurrences of open and close square and curly brackets ' [ ' , ' { ' , ' } ' , ' ] ' and backslash \ followed by any alphabet except n i.e except '\n' - the next line escape sequence.

So if my string is:

$str = '{[Conan\b\A\nE}]';

After using:

$str2 = preg_replace($pattern, $replace, $str);

I should get:

$str2 = 'Conan\nE';

I tried using regex following tutorial and online regex generators but I can't figure out $pattern. Any help will be very much appreciated.

🌐
PHP
php.net › manual › en › reference.pcre.pattern.syntax.php
PHP: PCRE regex syntax - Manual
ive never used regex expressions till now and had loads of difficulty trying to convert a [url]link here[/url] into an href for use with posting messages on a forum, heres what i manage to come up with: $patterns = array( "/\[link\](.*?)\[\/link\]/", "/\[url\](.*?)\[\/url\]/", "/\[img\](.*?)\[\/img\]/", "/\[b\](.*?)\[\/b\]/", "/\[u\](.*?)\[\/u\]/", "/\[i\](.*?)\[\/i\]/" ); $replacements = array( "<a href=\"\\1\">\\1</a>", "<a href=\"\\1\">\\1</a>", "<img src=\"\\1\">", "<b>\\1</b>", "<u>\\1</u>", "<i>\\1</i>" ); $newText = preg_replace($patterns,$replacements, $text); at first it would collect ALL the tags into one link/bold/whatever, until i added the "?" i still dont fully understand it...