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 ^...
Related
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(.*)$/'
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))
I have the following regex:
preg_match_all("/^\\d{1,3}(?:k|rb|ribu|(?:\\.\\d{3})+|\\d+)$/m", 'Ini harga barangnya ya sis #26.000 banget', $matches);
This works fine for extracting 26.000 however if I have 26,000 this doesn't work.
How do I modify this so that it works for dot and comma? I don't want to change the other semantics in the regex
Here's a working sample regex. I wanted so that it also matches 26,000 in that regex without breaking the other already matching regex
\d{1,3}(?:k|rb|ribu|(?:[,.]\d{3})+|\d+)
Try this.See demo.
http://regex101.com/r/nG1gU7/4
http://regex101.com/r/bL8nO3/4
I am trying to build a regex that would extract the values of pseudo-xml-tags (enclodes in{} instead of <>) and it doesn't work. Have verified the thing with RegexBuddy, my favourite rx-tool which captured quite correct, but when using it in my PHP-Code, I do not get a result.
So, w/o further ado, here's the problem:
$match=array();
$ret=preg_match('\{lang\s*=\s*[\"\']*?(.*?)[\"\']*?\s*/\}',"{lang='DE'/}xxxxlxlxlxl",$match);
Why is $match empty?
The pattern should be
/\{lang\s*=\s*[\"\']*?(.*?)[\"\']*?\s*\/\}/
^ ^
I've been using the following site to test a PHP regex so I don't have to constantly upload:
http://www.spaweditor.com/scripts/regex/index.php
I'm using the following regex:
/(.*?)\.{3}/
on the following string (replacing with nothing):
Non-important data...important data...more important data
and preg_replace is returning:
more important data
yet I expect it to return:
important data...more important data
I thought the ? is the non-greedy modifier. What's going on here?
Your non-greedy modifier is working as expected. But preg_match replaces all occurences of the the (non-greedy) match with the replacement text ("" in your case). If you want only the first one replaced, you could pass 1 as the optional 4th argument (limit) to preg_replace function (PHP docs for preg_replace). On the website you linked, this can be accomplished by typing 1 into the text input between the word "Flags" and the word "limit".
just an actual example of #Asaph solution. In this example ou don't need non-greediness because you can specify a count.
replace just the first occurrence of # in a line with a marker
$line=preg_replace('/#/','zzzzxxxzzz',$line,1);