Regex won't find the designated string - php

I lately got back in to regex and I'm working on a simple log highlighting system. Currently I have a specific problem with one expression.
For example, this is what appears in the log file:
15:49:19 - <img=ico_headshot> player1
00:00:53 - <img=ico_headshot> player2
This is what my regex string looks like:
/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\s\-\s<img=ico_headshot>\s(.*)/
I tried this in numerous regEx online testers and they show that the regex string and the string from the logs do match.
This is how it works in the file: https://gist.github.com/anonymous/5960ccbcf21832d727c1a097c0878cb1
Thanks for your help.

Try to use this regular expression.
By using Java:
"^[0-9:]+\\s-\\s(.*)\\s(.*)$"
By using PHP:
'/^[0-9:]+\s-\s(.*)\s(.*)$/'

Related

Match string with last part different

I have the following regex:
/{{trans-template-id:(\d)+}}/i
And it matches {{trans-template-id:7}} perfectly.
However, I want to modify it so it will match {{trans-template-hash:asdf1234}} as well. How would I modify it?
I have gotten this far, but I don't know regex well enough it seems:
/{{trans-template-(id:(\d)+|hash:[a-zA-Z0-9]+)}}/i
If you want to make a common group, you can use
{{trans-template-(id|hash):((?<=id:)\d+|(?<=hash:)\w+)}}
Regex Demo
You can also use branch reset like
{{trans-template-(?|id:(\d+)|hash:(\w+))}}
Regex Demo

^ symbol with regex and PHP not working

This simple regex expression:
~^(\d{6})~
Is not working on PHP preg_match. why?
preg_match('~^(\d{6})~', $filtered, $matchesAs);
The $filtered variable content is:
103031Theory and blahblah from blahblahblah421001279Martin Pascal, Michael TruthLSS
And I want to get the first appeareance of "103031" or any six-number group at the very start of the line. The regex expression is working properly at online testers, but when I try it with "preg_match" $matchesAs is empty. (\d{6}) works well without ^...

regex difference with php whitespace

I can't get this regex to work with PHP specifically the whitespace in the middle, the value or unit match group individually will match.
regex:
/(?<value>\d+\.?\d*)(\p{Z}|\s)(?<unit>(meters|mm))/
string to parse:
Cord Length:1.52 meters
try on http://www.phpliveregex.com/ it doesn't match.
http://www.phpliveregex.com/p/bV7
try on https://regex101.com/ it works fine
EDIT: still doesn't seem to be working on phpliveregex.com for me
http://www.phpliveregex.com/p/bV7
EDIT2: I have edited the string to parse.
Apart from the above comments, I would modify your regular expression as follows:
(?<value>\d+(?:\.\d+)?)\h+(?<unit>(?:meters|mm))

Need to reverse php regex and see what needs to be matched

I am not very good in RegEx and saying that I am editing a website written in php. One of it's functions are failing because of this regex:
preg_match("/^minus\((\-?[\d\.]+)\)$/i",$val,$m)
I know it should be something like minus() but it doesn't seems to find any matches as I can't figure it out what needs to be inside the brackets.
The regex is meant to match something like this:
minus(0.12)
minus(-0.12)
minus(.12)
However it is inaccurate, since it would also match:
minus(0.1.2)
Correct would be:
/minus\(-?\d*(\.\d+)?\)/
You can test it here: https://regex101.com/r/zN5xI8/1

Is this a PHP preg_match bug or am I doing something wrong?

I use the following regular expression to match valid domain names:
/^([a-z0-9]([a-z0-9]*-*[a-z0-9])*\.)+[a-z][a-z]+$/
This works fine. But when I replace part of it with a domain name to match the domain name itself and sub domains of it, it doesn't work. For example, if I use
/^([a-z0-9]([a-z0-9]*-*[a-z0-9])*\.)*mycarbrokedown.be$/
to match ns1.mycarbrokedown.be, preg_match returns 0.
I've used a couple online testers which confirm that my regular expression does match my string. Curiously, regextester.com doesn't return anything when I use the preg option.
All of this leads me to think that it's a bug in PHP. As I have no idea what's causing the bug, I haven't been able to find matching bug reports.
What's going on here?
Please try this regex and let me know of the results. It might be that you have whitespace or something else in front/the end of the string and that's the reason it doesn't match, so I removed ^ and &. Also this is a regex from RegexBuddy Library.
preg_match_all('/([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}/i', $subject, $result, PREG_PATTERN_ORDER);

Categories