I have created a regular expression.
([a-zA-Z<][/a-zA-Z0-9> .?!,;_<' -])[ | ][0-9][ | ]
I would like to use it on a subtitle file. e.g.:
65 00:04:33,417 --> 00:04:35,658 But I quit my old job so I could be with you.
66 00:04:35,937 --> 00:04:36,984 Do you miss it?
67 00:04:41,217 --> 00:04:42,423 No, I like my new job.
When I use this regex on regex101 or regexr.com then it works properly. But when I use in my PHP code then they have a different results.
$myString=file_get_contents("wwz.srt");
$formula="/([a-zA-Z<][\/a-zA-Z0-9> .?!,;_<'
-]*)[ |
][0-9]*[ |
]/";
preg_match_all($formula, $myString, $matches);
There is a part of the subtitle:
70 00:04:49,017 --> 00:04:50,348 Look who I found!
71 00:04:49,017 --> 00:04:50,348 Here comes the number 12 train!
72 00:04:50,417 --> 00:04:51,782 It's not Subway Sam, is it?
73 00:04:52,337 --> 00:04:54,578 You want more pancakes?
74 00:04:55,177 --> 00:04:56,542 Good.
75 00:04:56,697 --> 00:04:58,984 Go, go, go. Come on, scootch!
76 00:04:59,057 --> 00:05:00,821 - At least... - Move it on out.
77 00:05:00,897 --> 00:05:02,786 Hey! Plates in the sink!
78 00:05:02,857 --> 00:05:04,018 - Later! - Do that when we get back!
If I use this example in the regex101, this will be the first result:
Look who I found! 71
If I use this example in my php code with the same regex, this will be the first result:
Here comes the number 12
Where is the mistake?
When you copy a crafted regex from regex101.com in order to use it in your program you should use the code generator because it ensures that characters are escaped properly for being used in a string. In addition to this you have to make sure that you choose the correct flavor, because you might get a regex that is working online with regex101 but not inside your program because it uses a different regex engine.
Also, I think a more helpful website to test php regex would be http://www.phpliveregex.com/