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.
Related
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.
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?
"Hello" returns true
"12345" returns true
"Hello1" returns false
"123H" returns false
regex can possible check only letters and numbers except both?
or
Check function of PHP?
Regex:
/^[A-za-z]*|[0-9]*$/
Check for start of line, then any numbers of letter OR any number of numbers, then end of line.
A blank line will return true. If that's a problem change the * to a +.
You could use something like so: ^([A-Za-z]+)|([0-9]+)$. This will make sure that the string is either full of letters exclusively, or numbers exclusively, but not both.
You can check the regular expression here.
^(?=(?:\d+|[a-zA-Z]+)$)[a-zA-Z0-9]+$
Another variation.See demo.
https://regex101.com/r/sH8aR8/11
$re = "/^(?=(?:\\d+|[a-zA-Z]+)$)[a-zA-Z0-9]+$/m";
$str = "Hello\n12345\nHello1\n123H";
preg_match_all($re, $str, $matches);
I went through a few basics on preg match but its quite difficult when your new to it.
What im trying to do is search for this instance in a string
bug1234
it shouldnt be case sensitive so bug1234 or BuG1234 should work
it must be the word bug followed by any 4 numbers
there should be no spaces or anything in between bug1234 so bug-1234 should not be a match
it should ignore things like bug1234z and abug1234 so it must be bug1234 with nothing prefixing it or coming directly after it unless there is a space between then so "there is a problem with bug1234 that i cant solve" would be a match.
Just to clarify it can be any number not 1234 specifically but they must be 4 digits
Heres my lame attempt:
$file_string = $workdetails->text;
$file_string = strtolower($file_string);
$bugkey = "/bug[0-9]{4}/";
$nosey = preg_match($bugkey, $file_string);
if($nosey !== false)
{
echo "We have a match baby!!"
}
That just seemed to return all sorts, empty string,s string with no mention of the word bug
Try changing the regex to:
/\bbug[0-9]{4}\b/i
The \b modifier will only match on a word boundary so that makes sure it doesn't match things like abug1234, bug12345 or bug1234was bad. I also added the i modifier so it is case insensitive. You no longer need to use strtolower.
Also, preg_match typically returns an integer, and returns (int)0 if there is no match. It only returns FALSE on failure. Therefore you should change the match check to be:
if ($nosey > 0) {
// or just
if ($nosey) {
Try it with:
preg_match('/\b(bug\d{4})/i', $file_string, $match);
print_r($match);
The modifier i stands for case insensitive and the \b is for a whole word only (word boundary).
Try this: /(?ism)bug(?i-sm)[0-9]{4}/
Tested on the Regular Expression Test Tool
With data:
sdfsfsbUG1234cccs
Cheers!
Given the following string how can I match the entire number at the end of it?
$string = "Conacu P PPL Europe/Bucharest 680979";
I have to tell that the lenght of the string is not constant.
My language of choice is PHP.
Thanks.
You could use a regex with preg_match, like this :
$string = "Conacu P PPL Europe/Bucharest 680979";
$matches = array();
if (preg_match('#(\d+)$#', $string, $matches)) {
var_dump($matches[1]);
}
And you'll get :
string '680979' (length=6)
And here is some information:
The # at the beginning and the end of the regex are the delimiters -- they don't mean anything : they just indicate the beginning and end of the regex ; and you could use whatever character you want (people often use / )
The '$' at the end of the pattern means "end of the string"
the () means you want to capture what is between them
with preg_match, the array given as third parameter will contain those captured data
the first item in that array will be the whole matched string
and the next ones will contain each data matched in a set of ()
the \d means "a number"
and the + means one or more time
So :
match one or more number
at the end of the string
For more information, you can take a look at PCRE Patterns and Pattern Syntax.
The following regex should do the trick:
/(\d+)$/
EDIT: This answer checks if the very last character in a string is a digit or not. As the question https://stackoverflow.com/q/12258656/1331430 was closed as an exact duplicate of this one, I'll post my answer for it here. For what this question's OP is requesting though, use the accepted answer.
Here's my non-regex solution for checking if the last character in a string is a digit:
if (ctype_digit(substr($string, -1))) {
//last character in string is a digit.
}
DEMO
substr passing start=-1 will return the last character of the string, which then is checked against ctype_digit which will return true if the character is a digit, or false otherwise.
References:
substr
ctype_digit
To get the number at the end of a string, without using regex:
function getNumberAtEndOfString(string $string) : ?int
{
$result = sscanf(strrev($string), "%d%s");
if(isset($result[0])) return strrev($result[0]);
return null;
}
var_dump(getNumberAtEndOfString("Conacu P PPL Europe/Bucharest 680979")); //int(680979)