"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);
Related
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?
how would I avoid that the following :
$_SESSION['myVar']=preg_match("[^a-zA-Z]",'',$_SESSION['myVar']);
echo $_SESSION['myVar'];
displays
0
and instead it displays/outputs the var content ? preg_match gives out mixed type, but this shouldnt be the problem...
Why, is the value of the string itself not addressable with echo (by comapring its contents, it is OK)?
Formerly I had
$_SESSION['myVar']=ereg_replace("[^a-zA-Z]",'',$_SESSION['myVar']);
ant the output óf ereg_replace was correctly displayed the variable content.
PCRE in PHP need delimiters [docs] and you probably want preg_replace [docs]:
preg_replace("/[^a-zA-Z]/",'',$_SESSION['myVar']);
Assuming you had preg_replace, even then, the brackets ([...]) would be interpreted as delimiters and so the engine would literally try to match a-zA-Z at the beginning of the string and would not interpret the constructor as character class.
preg_match returns an int, not mixed: http://php.net/manual/en/function.preg-match.php
Use the matches parameter to get your matches.
The problem is that preg_match returns a Boolean, 1 if the pattern was matched, 0 if it didn't. preg_match simply matches occurrences, it doesn't replace them. Here's how you use preg_match:
$matched = array();
preg_match("/[^a-zA-Z]/", $_SESSION["myVar"], $matches);
print_r($matches); // All matches are in the array.
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.
I want to find the first matching string in a very very long text. I know I can use preg_grep() and take the first element of the returned array. But it is not efficient to do it like that if I only need the first match (or I know there is exactly only one match in advance). Any suggestion?
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.
Here's an example of how you can do it:
$string = 'A01B1/00asdqwe';
$pattern = '~^[A-Z][0-9][0-9][A-Z][0-9]+~';
if (preg_match($pattern, $string, $match) ) {
echo "We have matched: $match[0]\n";
} else {
echo "Not matched\n";
}
You can try print_r($match) to check the array structure and test your regex.
Side note on regex:
The tilde ~ in the regex are just delimiters needed to wrap around
the pattern.
The caret ^ denote that we are matching from the start
of the string (optional)
The plus + denotes that we can have one or
more integers that follow. (So that A01B1, A01B12, A01B123 will also
be matched.
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)