I'm using the following PCRE expression with preg_match to check if the value I want is a digit or not.
(?P<id>[\d]+)
It works, but now I want it to match the same conditions except if the whole content equals 0 (zero).
Example result
1 valid
10 valid
0 invalid
Expression context
#^(?P<controller>.*?|home)(?:/(?P<action>.*?|index)(?:/(?P<id>[\d]+))?)?$#uD
Can you try this?
#^(?P<controller>home)(?:/(?P<action>index)(?:/(?P<id>[1-9][\d]*))?)?$#uD
In your example, just using the digits, it's matching first, so obviously 0 gets captured. Assuming your match string is more complex than that, in which case the "1-9 once and 0-9 0 or more times" should do it for you.
See: http://regex101.com/r/yK6mR5
You can use this regex:
^(?!0+$)(?P<id>\d+)$
Online Demo: http://regex101.com/r/dB2eT3
UPDATE:
Working regex:
'#^(?P<controller>[^/]*|home)(?:/(?P<action>[^/]*|index)(?:/(?!0+$)(?P<id>\d+))?)?$#'
Related
:)
We would like to set a special condition (based on PHP Preg_match regular expression) to validates a number on our form.
That “number field” need, at first, only contain a max of 13 numbers (and only numbers. No letters or anything else).
The very first number need to be (only) “1” or “2” (not anything else)
The 4rd and 5rd number represent (the 2 numbers combinated) the “Month of birth” of someone, so the 4rd number need to be "0" or "1", and the 5rd need to be between "1" and "9".
Really appreciates if you can help us for that, to have the good “syntax” for the regular expression in PHP Preg_match to validates that field on our form! :)
Thanks to the community for your support and help!
Regards
Here is the literal regex pattern you have described to us:
^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
Sample script:
$input = "1231212345678";
if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
echo "MATCH";
}
This regex pattern says to:
^ from the start of the string
[12] match 1 or 2 as the first digit
\d{2} then match any digits in the 2nd and 3rd position
(?:0[1-9]|1[0-2]) match 01, 02, ..., 12 as the two digit month
\d{8} then match any other 8 digits
$ end of string
I have these two regular expression
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
^(9){1}[0-9]{9}+$
How can I combine these phrases together?
valid phone :
just start with : 0098 , +98 , 98 , 09 and 9
sample :
00989151855454
+989151855454
989151855454
09151855454
9151855454
You haven't provided what passes and what doesn't, but I think this will work if I understand correctly...
/^\+?0{0,2}98?/
Live demo
^ Matches the start of the string
\+? Matches 0 or 1 plus symbols (the backslash is to escape)
0{0,2} Matches between 0 and 2 (0, 1, and 2) of the 0 character
9 Matches a literal 9
8? Matches 0 or 1 of the literal 8 characters
Looking at your second regex, it looks like you want to make the first part ((98)|(\+98)|(0098)|0) in your first regex optional. Just make it optional by putting ? after it and it will allow the numbers allowed by second regex. Change this,
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
to,
^(?:98|\+98|0098|0)?9[0-9]{9}$
^ this makes the non-grouping pattern optional which contains various alternations you want to allow.
I've made few more corrections in the regex. Use of {1} is redundant as that's the default behavior of a character, with or without it. and you don't need to unnecessarily group regex unless you need the groups. And I've removed the outer most parenthesis and + after it as that is not needed.
Demo
This regex
^(?:98|\+98|0098|0)?9[0-9]{9}$
matches
00989151855454
+989151855454
989151855454
09151855454
9151855454
Demo: https://regex101.com/r/VFc4pK/1/
However note that you are requiring to have a 9 as first digit after the country code or 0.
$st1='dsdsdsd 97537 sdsdd dsddd';
$st2='fdsf 23e sdsd 434 432443454';
$st3='fdf97537 ds344dsddd';
I want to check whether a 5 digit number is available in a string.
st1-- has 5 digit number
st2--- not
st3-- has 5 digit number
A simple regex will do the job.
preg_match('/\d{5}/', $input)
See also http://www.php.net/preg_match
Try this regular expression with preg_match() or preg_match_all()
preg_match("/\b[^\d]*\d{5}[^\d]*\b/", $str);
Let's assume that each element to be checked if this is five digits number is separated by space in string. Therefore you may use explode function to convert string into array of substrings. next you can use is_numeric function to check if that is digit along with check if that sub string is five length long. Also you may use regular expression for that.
Here RegEx is far more better. As I see the #Matt's answer meets these requirements, my comments will be unnecessary.
I need to check input with preg_match, which must be of this format: xxx.xxx.xxx
The number of block can vary... These are all examples of valid inputs:
001
00a.00a
0fg.001
aaa.aaa.001
001.001.002.001.001.001
Well I could probably write a regexp something like:
^([\da-z]{3}\.?)+$
But here comes the problem with the quantifier of the period. I mean if I use '?' to match 0 or 1 times, it would also match even if skip the dots somewhere, eg:
000.001.0010az001
then, if I used {1} to match one time, it would match nothing, because the last block does not have a dot.
So I can't think what to think of... Please advice
You can use:
^[\da-z]{3}(?:\.[\da-z]{3})*$
/^(?:[\da-z]{3}\.)*[\da-z]{3}$/
Alright, so I want the user to be able to enter every character from A-Z and every number from 0-9, but I don't want them entering "special characters".
Code:
if (preg_match("/^[a-zA-Z0-9]$/", $user_name)) {
#Stuff
}
How is it possible for it to check all of the characters given, and then check if those were matched? I've tried preg_match_all(), but I didn't honestly understand much of it.
Like if a user entered "FaiL65Mal", I want it to allow it and move on. But if they enter "Fail{]^7(,", I want it to appear with an error.
You just need a quantifier in your regex:
Zero or more characters *:
/^[a-zA-Z0-9]*$/
One or more characters +:
/^[a-zA-Z0-9]+$/
Your regex as is will only match a string with exactly one character that is either a letter or number. You want one of the above options for zero or more or one or more, depending on if you want to allow or reject the empty string.
Your regular expression needs to be changed to
/^[a-zA-Z0-9]{1,8}$/
For usernames between 1 and 8 characters. Just adjust the 8 to the appropriate number and perhaps the 1.
Currently your expression matches one character
Please keep in mid that preg_match() and other preg_*() functions aren't reliable because they return either 0 or false on fail, so a simple if won't throw on error.
Consider using T-Regx:
if (pattern(('^[a-zA-Z0-9]{1,8}$')->matches($input))
{
// Matches! :)
}