PHP check that string has 2 numbers, 8 chars and 1 capital - php

I found lots of php regex and other options to determine string length, and if it contains one letter or one number, but how do I determine if a string has 2 numbers in it?
I am trying to validate a password that
Must have exactly 8 characters
One of them must be an Uppercase letter
2 of them must be numbers
Is there a one line regex solution for this?

if (preg_match(
'/^ # Start of string
(?=.*\p{Lu}) # at least one uppercase letter
(?=.*\d.*\d) # at least two digits
.{8} # exactly 8 characters
$ # End of string
/xu',
$subject)) {
# Successful match
(?=...) is a lookahead assertion. It checks if a certain regex can be matched at the current position, but doesn't actually consume any part of the string, so you can just place several of those in a row.

Related

Regex not working - minimum 1 uppercase, minimum 3 lowercase, minimum 1 special character from the defined group (more in description)

I need to create a regular expression with the next demands: Password need to have at least 8 characters and maximum 12 characters, at least 1 uppercase, at least 3 lowercase, at least 1 digit and at least 1 special character from the group (#*.!?$), without parentheses. The first character needs to be an uppercase or lowercase letter. Two consecutive same characters must not appear in the password.
I made this, but it doesn't work:
^(?=.{8,12}$)(([a-z]|[A-Z])(?=.*\d){1,}(?=.*[a-z]){3,}(?=.*[A-Z]){1,}(?=.*[!.?*$#])\2?(?!\2))+$
I tried to test it with Abcd123!, but it doesn't work. Can anyone explain where did I make a mistake, and what I actually did here?
You use a quantifier like {1,} for the lookahead which is not correct.
I think you meant to use the lookaheads like this:
^(?=.{8,12}$)(?=[^A-Z]*[A-Z])(?=\D*\d)(?=(?:[^a-z]*[a-z]){3})(?=[^\s#*.!?$]*[#*.!?$])(?!.*(.)\1)(?:[a-z]|[A-Z])[a-zA-Z0-9#*.!?$,]+$
About the pattern
^ Start of the string
(?=.{8,12}$) Assert lenght 8 - 12
(?=[^A-Z]*[A-Z]) Assert an uppercase char
(?=\D*\d) Assert a digit
(?=(?:[^a-z]*[a-z]){3}) Assert 3 lowercase chars
(?=[^\s#*.!?$]*[#*.!?$]) Assert special char
(?!.*(.)\1) Assert not 2 consecutive chars
(?:[a-z]|[A-Z]) Start with upper or lowercase char
[a-zA-Z0-9#*.!?$,]+ Match 1+ times any of the listed in the character class
$ Assert end of the string
Regex demo | Php demo

matching 8 digit of alphanumeric in a string

I wanted to use regular expression to check if a string has a word that contains 8 digit of alphanumeric character, ignoring uppercase and lowercase (meaning that 2HJS1289 and 2hjs1289 should match). I know I can use preg to do this, and so far I have this:
preg_match('/[A-Za-z0-9]/i', $string)
I am unsure however on how to limit it only to 8 digits/character scheme.
For exactly 8 char word you will need to use word boundaries: \b
preg_match('/\b[A-Z\d]{8}\b/i', $string)
Try
preg_match('/\b([A-Z0-9]{8})\b/i', $string)
The {8} matches exactly 8 times. I added the capturing group (the parentheses), in case you needed to extract the actual match.
You can also use {min,max} to match the pattern repeating between min and max times (inclusive, I think). Or you can leave one of the parameters out to leave it open ended. Eg {min,} to match at least min times
[a-zA-Z0-9] - will match upper or lowercase letters or numbers
{8} - will specify to match 8 of the preceeding token
put it together:
preg_match('/([A-Za-z0-9]{8})/i', $string)
example

php regex - find uppercase string with number and spaces in text

I want to write php regular expression to find uppercase string , which can also contain one number and spaces, from text.
For example from this text "some text to contain EXAM PL E 7STRING uppercase word" I want to get string- EXAM PL E 7STRING ,
found string should start and end only with uppercase, but in the middle, without uppercase letters can also contain(but not necessarily ) one number and spaces. So, regex should match any of these patterns
1) EXAMPLESTRING - just uppercase string
2) EXAMP4LESTRING - with number
3) EXAMPLES TRING - with space
4) EXAM PL E STRING - with more than one spaces
5) EXAMP LE4STRING - with number and space
6) EXAMP LE 4ST RI NG - with number and spaces
and with total length string should be equal or more than 4 letters
I wrote this regex '/[A-Z]{1,}([A-Z\s]{2,}|\d?)[A-Z]{1,}/', that can find first 4 patterns, but I can not figure it out to match also the last 2 patterns.
Thanks
There is a neat trick called a lookahead. It just checks what is following after the current position. That can be used to check for multiple conditions:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])(?!(?:[A-Z\s]*\d){2})[A-Z][A-Z\s\d]*[A-Z]/'
The first lookaround is actually a lookbehind and checks that there is no previous uppercase letter. This is just a little speedup for strings that would fail the match anyway. The second lookaround (a lookahead) checks that there are at least four letters. The third one checks that there are no two digits. The rest just matches then a string of the allowed characters, starting and ending with an uppercase letter.
Note that in the case of two digits this will not match at all (instead of matching everything up to the second digit). If you do want to match in such a case, you could incorporate the "1 digit" rule into the actual match instead:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])[A-Z][A-Z\s]*\d?[A-Z\s]*[A-Z]/'
EDIT:
As Ωmega pointed out, this will cause problems if there are less then four letters before the second digit, but more after that. This is actually quite tough, because the assertion needs to be, that there are more than 4 letters before the second digit. Since we do not know where the first digit occurs in those four letters, we have to check for all possible positions. For this I would do away with the lookaheads altogether, and simply provide the three different alternatives. (I will keep the lookbehind as an optimization for non-matching parts.)
'/(?<![A-Z])[A-Z]\s*(?:\d\s*[A-Z]\s*[A-Z]|[A-Z]\s*\d\s*[A-Z]|[A-Z]\s*[A-Z][A-Z\s]*\d?)[A-Z\s]*[A-Z]/'
Or here with added comments:
'/
(?<! # negative lookbehind
[A-Z] # current position is not preceded by a letter
) # end of lookbehind
[A-Z] # match has to start with uppercase letter
\s* # optional spaces after first letter
(?: # subpattern for possible digit positions
\d\s*[A-Z]\s*[A-Z]
# digit comes after first letter, we need two more letters before last one
| # OR
[A-Z]\s*\d\s*[A-Z]
# digit comes after second letter, we need one more letter before last one
| # OR
[A-Z]\s*[A-Z][A-Z\s]*\d?
# digit comes after third letter, or later, or not at all
) # end of subpattern for possible digit positions
[A-Z\s]* # arbitrary amount of further letters and whitespace
[A-Z] # match has to end with uppercase letter
/x'
That gives the same result on Ωmega's lengthy test input.
I suggest to use regex pattern
[A-Z][ ]*(\d)?(?(1)(?:[ ]*[A-Z]){3,}|[A-Z][ ]*(\d)?(?(2)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(\d)?(?(3)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(?:\d|(?:[ ]*[A-Z])+[ ]*\d?))))(?:[ ]*[A-Z])*
(see this demo).
[A-Z][ ]*(?:\d(?:[ ]*[A-Z]){2}|[A-Z][ ]*\d[ ]*[A-Z]|(?:[A-Z][ ]*){2,}\d?)[A-Z ]*[A-Z]
(see this demo)

PHP regular expression for given condition [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regex , extract phone number from text/html
I have following requirement. There is a phone number field.
Its length should be between 8 and 10
It can contain only _ and white spaces
First and last elements should be digits
_ and white spaces should be in between 2 digits
Can I write a regular expression to validate the above things using php?
Example for valid entry: 231_901 347
Example for invalid entry: _123 567, 345__123
Also if possible, can you please write down the preg_match expression.
Here is the code which I tried:
$subject = "_012_345 69";
$pattern = '/[\d]{1}.[\d\s_]{5,10}.[\d]{1}$/';
$matches = preg_match($pattern, $subject);
echo($matches);
It should return 0 but it returns 1.
^(?=.{8,10}$)\d+(?:[ _]\d+)*$
Explanation:
^ # Start of string
(?=.{8,10}$) # Assert length 8-10
\d+ # One or more digits
(?: # followed by
[ _] # one space or underscore
\d+ # one or more digits
)* # zero or more times.
$ # End of string
Use regex pattern
^(?=.{8,10}$)(?=\d)(?=.*\d$)(?:\d|(?<=\d)[\s_](?=\d))+$
Explanation >>
^
Start if line/string
(?=.{8,10}$)
String size is 8-10 characters
(?=\d)
First character is digit
(?=.*\d$)
Last character is digit
(?:\d|(?<=\d)[\s_](?=\d))+
Allowed characters are (a) digits or (b) whitespace or underscore between digits
$
End of line/string
This meets all your requirements as I understand them:
^(?:\d[ _]*){7,9}\d$
As for those false positives you're getting, you just left off the start anchor (^). But even with anchor added it matches things like
1_________2
and
01234567890123
I'm assuming those are invalid because you meant there have to be eight to ten digits, not that the overall length should be eight to ten.

PHP simple regex

I want to validate a string only if it contains '0-9' chars with length between 7 and 9.
What I have is [0-9]{7,9} but this matches a string of ten chars too, which I don't want.
Thanks.
You'll want to use ^[0-9]{7,9}$.
^ matches the beginning of the string, while $ matches the end.
If you want to find 7-9 digit numbers inside a larger string, you can use a negative lookbehind and lookahead to verify the match isn't preceded or followed by a digit
(?<![0-9])[0-9]{7,9}(?![0-9])
This breaks down as
(?<![0-9]) ensure next match is not preceded by a digit
[0-9]{7,9} match 7-9 digits as you require
(?![0-9]) ensure next char is not a digit
If you simply want to ensure the entire string is a 7-9 digit number, anchor the match to the start and end with ^ and $
^[0-9]{7,9}$

Categories