I hate to ask that kind of question because the answer is either 'I am stupid' or 'there is strange problem with my computer'... (and the first one is probably the right one) But I am stuck on that :
$matches = preg_grep("(.+)","ThisIsATest");
error_log(count($matches), 3, "php.log");
The log gives me 0, no matter what I give as a pattern... I can't understand why this $matches variable is always empty !
preg_grep is not for searching through a string, but an array of strings... You should probably use preg_match instead.
This is the error you should be getting:
preg_grep() expects parameter 2 to be array, string given
This is a way to return one match (and you will only have 1 in this case) with preg_match:
preg_match("(.+)","ThisIsATest", $matches);
print_r($matches);
See IDEONE demo
To access the value using $matches[0], you need to use preg_match_all:
preg_match_all("(.+)","ThisIsATest", $matches);
print_r($matches[0]);
See another demo
It seems that you are just using the wrong method:
if (preg_match('/(.+)/', "ThisIsATest")) {
# Successful match
} else {
# Match attempt failed
}
Related
I'm new to regex and I am really bad at it.
I've been trying to solve this problem but still can't get the result. So, I'm hoping that someone is able to assist me. thanks!
$str = "/tqrfq_58533_13";
preg_match_all('/\d+(?>=_)*/', $str, $matches);
print_r($matches); // gets 58533, 13
but I only want '58533' and not both numbers. So I want the array of $matches to return '58533' as the only number
Use /(?<=_)(\d+)(?=_)/ as pattern in preg_match() that match digits between _
$str = "/tqrfq_58533_13";
preg_match('/(?<=_)(\d+)(?=_)/', $str, $matches);
echo $matches[0];
// 58533
Check result in demo
Also you can use preg_replace() if you don't want to get array as result
echo preg_replace('/.*?_(\d+)_.*/', "$1", $str);
// 58533
preg_match_all('/\d+(?=_)/', $str, $matches);
If you want to get only one number, remove * part since it means the result will be more than one. AFAIK, there is no such things like (?>=_). I use (?=_) to indicate that _ immediately follow the number.
You can see this link for more clarification.
I know this type of questions have been asked before but none solves my problem.
I want to capture numeric parts of this string INFORMATICS&SYSTEMS-58600 i.e. 58600.
I am trying to do substr(INFORMATICS&SYSTEMS-58600,-5) which returns ATICS which is substr of first part of string INFORMATICS but I want the last part.
Wherever & is appearing this is behaving same.
I know its a very basic mistake but what ??? I cant figure out.Please help me out.
$str = 'INFORMATICS&SYSTEMS-58600';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
Can refer Extract numbers from a string
Actully PHP substr is working fine.
1. I was passing this text as url query in ajax i.e. get_data.php?dept ='informatics& system' so anything after & was treated as second parameter.
I found this nice answer on link to pass ajax parameters in url as encoded.
The regex in this code matches a number at the end of a string.
<?php
$str = "INFORMATICS&SYSTEMS-58600";
$matches = array();
preg_match("/\d+$/", $str, $matches);
foreach($matches as $match) {
echo $match;
}
?>
Output:
58600
Please explain to me when $string will be true. I cannot find all information by Google.
preg_match('#^[0-9a-f]{32}$#', $string)
{32} means $string must contain 32 chars? [0-9a-f] is mean that only numeric and lower case must be in $string?
I have validation where I check if preg_match is true. But I cannot understand $string template.
$string is the subject you are searching on.
$pattern = '/0x[\da-f]/i';
preg_match($pattern, $subject, $matches);
print_r($matches);
Read the docs. As for return values of this function, if you just care for existence of a match...
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
I'm pretty sure you can't use the # symbol for regex, you need a forward slash. I have:
if(preg_match("/[^0-9]/", $data)){$data=null}
This will evaluate if the input data is a number. There is A LOT you can do with regex ... what is it you need to do? Perhaps a more specific question about what you need it to do and what you have tried?
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+)
Just one simple, specific question:
I've got the string {var1}12345{var2}, and I want to get the variable names used.
if (preg_match("/{([a-zA-Z0-9]*)}/g", $url, $matches)) {
print_r($matches);
}
If I remove the global flag, it works, but I only get the first variable, as expected. Why isn't it working with a global flag? It works when I'm testing it with the Regex Tester
From PHP: preg_match:
preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred.
Use preg_match_all to fetch several matches:
if (preg_match_all("/{([a-zA-Z0-9]*)}/", $url, $matches)) {
print_r($matches[1]);
}
This should do the trick (in case you need variables in format {name}):
$url = "{var1}12345{var2}";
if (preg_match_all("/{[a-zA-Z0-9]*}/", $url, $matches)) {
print_r($matches);
}