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
Related
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 ^...
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'm a regex-noobie, so sorry for this "simple" question:
I've got an URL like following:
http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx
what I'm going to archieve is getting the number-sequence (aka Job-ID) right before the ".aspx" with preg_replace.
I've already figured out that the regex for finding it could be
(?!.*-).*(?=\.)
Now preg_replace needs the opposite of that regular expression. How can I archieve that? Also worth mentioning:
The URL can have multiple numbers in it. I only need the sequence right before ".aspx". Also, there could be some php attributes behind the ".aspx" like "&mobile=true"
Thank you for your answers!
You can use:
$re = '/[^-.]+(?=\.aspx)/i';
preg_match($re, $input, $matches);
//=> 146370543
This will match text not a hyphen and not a dot and that is followed by .aspx using a lookahead (?=\.aspx).
RegEx Demo
You can just use preg_match (you don't need preg_replace, as you don't want to change the original string) and capture the number before the .aspx, which is always at the end, so the simplest way, I could think of is:
<?php
$string = "http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx";
$regex = '/([0-9]+)\.aspx$/';
preg_match($regex, $string, $results);
print $results[1];
?>
A short explanation:
$result contains an array of results; as the whole string, that is searched for is the complete regex, the first element contains this match, so it would be 146370543.aspx in this example. The second element contains the group captured by using the parentheeses around [0-9]+.
You can get the opposite by using this regex:
(\D*)\d+(.*)
Working demo
MATCH 1
1. [0-100] `http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-`
2. [109-114] `.aspx`
Even if you just want the number for that url you can use this regex:
(\d+)
<h1>title 1</h1 w:id="0"/><p>content</p><h1>title 2</h1 w:id="1"/>...
I want to replace w:id="0"/ from </h1 w:id:="0 or 1 ect "/>
I use this code:
preg_replace("</h1 (.*?)>",'',$html)
But it doesn't work anymore
try this
preg_replace("/<\/h1 (.*?)>/i",'</h1>',$html);
You are missing the delimiters on your regex.
A regex needs a 'starting mark' and a similar 'finishing mark', so PHP can interpret it's content as the match, with all it's flags.
Without the recognized delimiters, it's impossible to diferenciate between simple text and a regex.
Try this regex:
#</h1 (.*?)>#
Or:
~</h1 (.*?)>~
PHP supports a few more delimiters, like <> and /.
As a side note, I would suggest the following regex:
~</h1( [^>]+)?>~i
I'm having a little problem with my Regex
I've made a custom BBcode for my website, however I also want URLs to be parsed too.
I'm using preg_replace and this is the pattern used to identify URLS:
/([\w]+:\/\/[\w-?&;#~=\.\/\#]+[\w\/])/is
Which works great, however if a URL is within a [img][/img] block, the above pattern also picks it up and produces a result like this:
//[img]http://url.com/toimg.jeg[/img] will produce this result:
<img src="<a href="http://url.com/toimg.jeg" target="_blank">/>
//When it should produce:
<img src="http://url.com/toimg.jeg"/>
I tried using this:
/([^"][\w]+:\/\/[\w-?&;#~=\.\/\#]+[\w\/][^"])/is
With no luck.
Any help will be appreciated.
Edit:
For solution See the 2nd comment on stema's answer.
Try this
(?<!href=")(\b[\w]+:\/\/[\w-?&;#~=\.\/\#]+[\w\/])
See it here on Regexr
To make it more general you can simplify your lookbehind to check only for "=""
(?<!=")(\b[\w]+:\/\/[\w-?&;#~=\.\/\#]+[\w\/])
See it on Regexr
(?<!href=") is a negative lookbehind assertion, it ensures that there is no "href="" before your pattern.
\b is a word boundary that anchors the start of your link to a change from a non word to a word character. without this the lookbehind would be useless and it would match from the "ttp://..." on.