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]
https://regex101.com/r/8xZ0a5/1
Works fine, but in my code it does nothing
var_dump(preg_replace($regex,'',$input_text)) - i get same result, no matches were replaced
input_text contains the same string as i put on website
Where's the issue?
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?
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.
Your regex pattern needs some delimiters.
if(preg_match("#(\d{1,2})\:(\d{2})#", "5:00", $matches) == 1) echo "works";
else echo "don't work";
You need to put your regular expression within delimiters:
if(preg_match("/(\d{1,2}):(\d{2})/", "5:00", $matches) == 1) echo "works";
else echo "don't work";
Also, you don't need to escape the :, but it works either way.
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.