Php find string with regex - php

I've read multiple tutorials on regex but it just won't stick in my head. I can never get my patterns to work. Hope someone can help.
I have a php variable ($content) where I need to find a certain pattern that looks like this
[gallery::name/of/the/folder/]
I would like to search:
- starting with "[gallery::"
- any other character (variable length)
- ending with "]"
So far, in PHP I have:
preg_match('/\[gallery\:/', $content, $matches, PREG_OFFSET_CAPTURE);
I can find [gallery: but that's it.
I would like to be able to find the rest (:name/of/the/folder/])
Any help is appreciated!
Thanks!

Try capturing it:
preg_match("/\[gallery::(.*?)]/", $content, $m);
Now $m is an array:
0 => [gallery::/name/of/the/folder/]
1 => /name/of/the/folder/

change your regex to
'/\[gallery::([A-Za-z\/]+)\]/'
Since I put the folder/path part in parenthesis, you should get a capture group out of it.

Related

How to get a number from a html source page?

I'm trying to retrieve the followed by count on my instagram page. I can't seem to get the Regex right and would very much appreciate some help.
Here's what I'm looking for:
y":{"count":
That's the beginning of the string, and I want the 4 numbers after that.
$string = preg_replace("{y"\"count":([0-9]+)\}","",$code);
Someone suggested this ^ but I can't get the formatting right...
You haven't posted your strings so it is a guess to what the regex should be... so I'll answer on why your codes fail.
preg_replace('"followed_by":{"count":\d')
This is very far from the correct preg_replace usage. You need to give it the replacement string and the string to search on. See http://php.net/manual/en/function.preg-replace.php
Your second usage:
$string = preg_replace(/^y":{"count[0-9]/","",$code);
Is closer but preg_replace is global so this is searching your whole file (or it would if not for the anchor) and will replace the found value with nothing. What your really want (I think) is to use preg_match.
$string = preg_match('/y":\{"count(\d{4})/"', $code, $match);
$counted = $match[1];
This presumes your regex was kind of correct already.
Per your update:
Demo: https://regex101.com/r/aR2iU2/1
$code = 'y":{"count:1234';
$string = preg_match('/y":\{"count:(\d{4})/', $code, $match);
$counted = $match[1];
echo $counted;
PHP Demo: https://eval.in/489436
I removed the ^ which requires the regex starts at the start of your string, escaped the { and made the\d be 4 characters long. The () is a capture group and stores whatever is found inside of it, in this case the 4 numbers.
Also if this isn't just for learning you should be prepared for this to stop working at some point as the service provider may change the format. The API is a safer route to go.
This regexp should capture value you're looking for in the first group:
\{"count":([0-9]+)\}
Use it with preg_match_all function to easily capture what you want into array (you're using preg_replace which isn't for retrieving data but for... well replacing it).
Your regexp isn't working because you didn't escaped curly brackets. And also you didn't put count quantifier (plus sign in my example) so it would only capture first digit anyway.

Complicated RegEx: get all strings that end with ":any-number-here" between brackets

I got a very specific regex to build and as a newbie I never tried something like this, since its very specific, I am finding difficult to get some nice examples.
I want to get every part of a string that ends with colon and any number, inside brackets. This may be confusing, so I'll get examples of what it should catch.
This should match:
[hello everyone example string:23]
[http://www.google.com:9234523]
[the temperature for today is:783]
this don't
[hello everyone example string:]
[hello everyone example string23]
So far I could get this to find all inside brackets with this php code:
preg_match_all("/\[[^\]]*\]/", $string, $result);
Any tips? Thanks :D
The following regex should work:
\[.*:\d+\]
You can test it here for example:
http://regexpal.com/
So the following should work for you:
preg_match_all("/\[.*:\d+\]/", $string, $result);
in php, the regex should start and end with an unique delimiter, here it's "/" but you can use "|", "#" or anything else.

preg_replace with Regex - find number-sequence in URL

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

find url with regex on text

there are a lot of topics like this one but i don't know what the error i tried a lot
so this is the original text
onclick="NewWindow('http://google.com','name','800','600','yes');return false">
this is my code
$re1='(onclick)';
$re2='(=)';
$re3='(.)';
$re4='(NewWindow)';
$re5='(\\()';
$re6='(.)';
$re7='((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';
$c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7."/is", $txt, $matches);
print_r($matches);
any one can help me to get the url using regular expression and php??
what is the wrong with this code?
Regards
preg_match("/NewWindow\('([^']*)'/",$txt, $matches);
matches[1] contains the url
is it what you need ?
(edit: put in code block because a parenthesis was not escaped correclty
This should work:
preg_match("/onclick=\"NewWindow\('(.*)','n/",$txt,$matches);
I'd use non-greedy matching for this:
preg_match("/onclick=\"NewWindow\('(.*?)'/", $txt, $matches);
Based on your description, the regex I would use, would be:
/(?<=NewWindow\(\').*(http://|https://)[^\'\"]*/i
or
/(?<=onclick=\"NewWindow\(\').*(http://|https://)[^\'\"]*/i
A great tool for testing your regex is: http://gskinner.com/RegExr/
It outputs just the url and only does so if it is preceded by "NewWindow('" in the first example or "onclick="NewWindow('", which means, in your case, 'http://google.com').

PHP Preg (regex) extracting last value from a string? not as simple as I thought

I'm trying to use PHP preg to extract the integer at the end of the string below, in the example its "4", but could be any number coming after "INBOX/", also the "testtester1010#mydomain.com" is a variable so it could be any address
STRING I'M EXTRACTING FROM:
/m/testtester1010#mydomain.com/folder/INBOX/4
/STRING
I've been going in circles with this, I guess I don't really understand how to do this and the regex examples I am finding in my searches just don't seem to address something like this, I would greatly appreciate any help..
ps. If anyone knows of a good regex software for building queries like this (for extraction) I would appreciate letting me know as I have spent countless hours lately with regex and still haven't found a software that seems to help in this.. thanks
Use:
#/([^/]*)$#
preg_match('#/([^/]*)$#', $str, $matches);
We first check for a slash. Then the capturing group is zero or more non-slash characters. Then, the end of the string. $matches[1] holds the result.
Why not simply match \d+$ - this will match any trailing number.
if (preg_match('/\d+$/', $subject, $match)) {
$result = $match[0];
} else {
$result = "";
}
If you want to match anything (even it it's not a number) after the last slash, just use [^/]+$ instead:
preg_match('#[^/]+$#', $subject, $match)

Categories