Cannot understand preg_match pattern - php

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?

Related

preg_match gets a number, which doesn't exist in the subject string

I'm trying to use preg_match(), but can't figure out a problem.
$pattern = "/\_image\_([0-9])\.jpg/";
$subject = "/image/2_image_2.jpg";
$Id = preg_match($pattern, $subject);
dd($Id);
I'm trying to get the second 2 (not the first one), but the result is 1, which doesn't exist in the subject.
I'm new to regex, clueless as to why this happens.
Any advice would be appreciated.
1 means a regex expression found a match in the input string. See preg_match documentation:
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
You can use the following code then:
$pattern = '/_image_([0-9])\.jpg/';
$subject = "/image/2_image_2.jpg";
if (preg_match($pattern, $subject, $matches))
echo $matches[1];
See the IDEONE demo
Main points:
Use single quoted literal to define the regex (it is not a problem as PHP treats unknown escape sequences as \ and a symbol, but that might not be good for the internals)
Pass the 3rd argument $matches to the preg_match so as to be able to access the matched/captured values
Check if there is a match before accessing the captured value
Group 1 (captured value) can be accessed via $matches[1].

Correct regex for this pattern

I've got some issues understanding this regex.
I tried doing a pattern but does not work like intended.
What I want is [A-Za-z]{2,3}[0-9]{2,30}
That is 2-3 letters in the beginning and 2-30 numbers after that
FA1321321
BFA18098097
I want to use it to validate an input field but can't figure out how the regex should look like.
Can any one that can help me out even explain a bit about it?
Your regex is correct - just make sure to surround it with / in PHP, and perhaps ^, $ if you want it to strictly match the entire string (no extra characters before/after).
$pattern = "/^[A-Za-z]{2,3}[0-9]{2,30}$/"
$found = preg_match($pattern, $your_str);
From the PHP documentation:
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

Using delimiters with preg_match

I am having difficulties to understand preg_match function.An e.g is way better
$subject="XY=abC%3Fedr%3Damp;35"
I am trying to extract
bC%3Fed
using preg_match and store it in variable
if(preg_match($pattern, $subject, $matches))
{
$string = $matches[1];
}
echo $string;
Here are the different variation that i use for $pattern
I want to use # as a delimeter
#bC(.*?)#
#bC.*?#
I just don't understand why its not working , i guess something is wrong in the $pattern.
Please don't use complicated regex and try to fix my attempt as the aim here is to understand how preg_match works and what is wrong here.
Regards
Using # as the delimiter is OK, but the regex is wrong. I guess you want:
#(bC.*?)r# // matches #bC and the following characters unless and 'r' (see comments)
A good starting point to learn the regex syntax is the PCRE manual
Example:
$subject="XY=abC%3Fedr%3Damp;35";
$pattern="#(bC.*?)r#";
preg_match($pattern, $subject, $matches);
$string = $matches[1];
echo $string; // bC%3Fed
The ? after .* switches the greediness of the pattern. By default patterns are greedy, they try to find the longest match. So you .*? means any char, any count, smallest match. Because here is nothing after that will anchor it, the smallest possible match is an empty string.

Match only the first set of numbers in a string

I need to retrieve the first set of numbers from a string, but I'm not sure how.
I have the following, which I was expecting to pick each set of numbers so that I could then pick the first key from the $matches array, but it literally matches only the first number.
In this example I'd be looking for '123'. Can someone please let me know how to do this with RegEx (or a better way if RegEx is not best for the job). Thanks.
$e = 'abc 123,456,def, 789-ab-552'; // Just a random example
$pattern = "/[0-9]/";
preg_match($pattern, $e, $matches);
You must add a quantifier:
$pattern = "/[0-9]+/";
+ means one or more
You can find an ajax regex tester for php here and more informations about regular expressions here.

php limit regex match with brackets doesn't work

return preg_match('/^([\d\p{Hebrew}]*\p{Hebrew}[\d\p{Hebrew}]*){1,64}$/iu', $str);
When trying the code above, the function returns true to strings larger than 64.
What is wrong here?
I am not sure what s wrong with your expression (I can reproduce it with ascii chars), but this is working
/^(?=.{1,64}$)([\d\p{Hebrew}]*\p{Hebrew}[\d\p{Hebrew}]*)$/
Remove the check for the length at the end.
Add (?=.{1,64}$) at the beginning. This is a positive look ahead, that just checks if the whole string is between 1 and 64 chars long. If yes it checks the pattern, if no the result is False.
See here on Regexr
preg_match('/^([\d\p{Hebrew}]*\p{Hebrew}[\d\p{Hebrew}]*){1,64}$/iu', $str, $matches);
return $matches;
it returns only if string is found, you have to fill third parameter ($matches) and return it.

Categories