regex difference with php whitespace - php

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))

Related

Regex with double negative matches

Given a series of strings:
error.user
success
success.user
success.admin
I want to write a regex that will match anything not starting with error, and that also doesn't have .user in it. So for this list, success and success.admin
What I've got so far is: /^((?!error)\w*)((?!\.*user)\w*)/
The first part: ((?!error)\w*) is working fine, and narrowing down the matches to just strings that start with success. For some reason the second part: ((?!\.*user)\w*) is doing precisely nothing. I think the first part is matching too much.
I'm doing this in PHP/PCRE
Here's my regex101.com link: https://regex101.com/r/l2sZru/1
You need to fix your negative regex like this:
^(?!error|.*\.user)[\w.]+$
RegEx Demo
Here (?!error|.*\.user) will assert failure if error is at the start OR if .user` is found anywhere in the input.
(?!\.*user) in your regex means assert failure when input has 0 or more DOTs followed by user at the start only.

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 ^...

detecting dot or comma in a regex

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

Need php regex between 2 sets of chars

I need a regular expression for php that outputs everything between <!--:en--> and <!--:-->.
So for <!--:en-->STRING<!--:--> it would output just STRING.
EDIT: oh and the following <!--:--> nedds to be the first one after <!--:en--> becouse there are more in the text..
The one you want is actually not too complicated:
/<!--:en-->(.*?)<!--:-->/gi
Your matches will be in capture group 1.
Explanation:
The .*? is a lazy quantifier. Basically, it means "keep matching until you find the shortest string that will still fit this pattern." This is what will cause the matching to stop at the first instance of <!--:-->, rather than sucking up everything until the last <!--:--> in the document.
Usage is something like preg_match("/<!--:en-->(.*?)<!--:-->/gi", $input) if I recall my PHP correctly.
If you have just that input
$input = '<!--:en-->STRING<!--:-->';
You can try with
$output = strip_tags($input);
Try:
^< !--:en-- >(.*)< !--:-- >$
I don't think any of the other characters need to be escaped.
<!--:en--\b[^>]*>(.*?)<!--:-->
This will match the things between your tags. This will break if you nest your tags, but you didnt say you were doing that :)

Categories