Laravel - validate only digits with fixed length and required space - php

I am writing a regular expression to validate a zip code, where it should have exactly a length of 6 characters, the first 3 characters are digits, the last 2 also, but the character 4 should be a space.
Here some examples:
"123456" is not valid because no space in character 4.
"123 45" is valid.
"123 4" is not valid because the length is 5 instead of 6.
Here what I wrote:
/^[0-9 ]{6,6}$/
It works fine, just in this code above, the space is not required (but it should be).

You may use
/^\d{3} \d{2}$/
It matches 3 digits, space, 2 digits strings. See the regex demo.
Details
^ - start of string
\d{3} - 3 digits (\d matches an ASCII digit in PHP regex by default, same as [0-9])
- space
\d{2} - 2 digits
$ - end of string
Note that {6,6} limiting quantifier is the same as {6}.

You were close but you missed some things.
If you changed your regex to this, it should have worked.
^[0-9]{3}\s[0-9]{2}$
Or you could also use
^\d{3}\s\d{2}$
Note that when you say {6,6}, it means the preceding expression must occur between m and n times. You could just give it as {6}, although your regex is wrong, it's just something to know.
Hope it helps :)

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

Regex - Find 8 digit number in string

I want to extract an 8 digit number from a string using regex.
Test strings are:
hi 82799162
236232 (82342450)
test data 8979
Required respective output should be
82799162
82342450
null
I have tried following code:
preg_match('/[0-9]{8}$/', $string, $match);
preg_match('/\d{8}$/', $string, $match);
But neither retrieves the number from 236232 (82342450).
If a regex is to capture exactly 8 digits, is must contain:
\d{8} as a central part,
a "before" condition, ensuring that no digit occurs before your match,
an "after" condition, ensuring that no digit occurs after your match.
One of possible solutions is to use negative lookbehind / lookahead:
(?<!\d)\d{8}(?!\d)
Another option is word boundary assertions (at both ends):
\b\d{8}\b
I think, regex like [0-9]{8} is not enough, as it captures also
first 8 digits from a longer sequence of digits.
Are you happy with that?
The problem is with your $ sign, and it is used to indicate the end of your expression. So basically, with that expression, you are looking for a string which ends with a 8 digit number. But in your second test string; '236232 (82342450)', ends with a bracket, and therefore it doesn't match the criteria (does not end with a number).
So remove the trailing $ and it will work.
preg_match('/[0-9]{8}/',$string,$match);
Hope it helps!!

regex for /command followed by number

Using the following regex doesn't work to validate /command number.
Here's what format of numbers I need to "validate"
/command 1
/command 0.5
/command 0.12345678
As you may see the value needs to be positive and the decimals are maximal 8.
I've done some research and found:
\/command\s?[\S]
But this work only for /command 1.
\/command\s?[\S] here [\S] will match only one non-space character so you can use and nothing else.
/command 1 // 1 : match one non-space
/command 0.5 // 0.5 :more than one non-space character so won't match
/command 0.123456789 // won't match
\/command\s?\S(\.\S{1,8})?
(\.\S+)? : ? match zero or one
(\.\S{1,8}) match . and 1 - 8 non-space character
more specifically for digits use
To define max 8 digit limit , use \d{1,8}
^\/command\s?\d(\.\d{1,8})?$
Note : if you want to match more digits before . e.g /command 123.5 then use
^\/command\s?\d+(\.\d{1,8})?$
as suggested by #jen and #serge
When you want to validate an entire string the first thing to remember is to enclose your pattern between the start and end of the string anchors: \A...\z
About the number with 8 decimals max there's nothing particular to say except that if you don't want a trailing dot you need to use an optional group and the correct quantifier: \d+(?:\.\d{1,8})?
Note also that you are free to change the pattern delimiter with an other character. This way you don't have to escape the slash that isn't a special regex character.
Result:
$pattern = '~\A/command \d+(?:\.\d{1,8})?\z~';
(feel free to make the space optional if needed)
This is because you missed '+' in [\S]+
But the above will not distinguish between numbers and other symbols.
To pick on 'numbers', you can use something like the following in 'perl'-like regex:
/\/command\s*[0-9.]+/

regex patterns - noob in PHP

I have been testing and googling but still could'nt work out pattern to validate comma separated numbers.
9 digits long, numbers only, no spaces, leading number for each digit cannot be zero
Tried
^(?:\s*\d{9}\s*(?:,|$))+$
but no go
Note that commas are required since the input file should between 3 up to 20 (max) comma separated integers
You mentioned that there should be no spaces, but you used \s* (0+ whitespaces) in your pattern. Also, (?:,|$) matches a , or end of string, so your pattern allows a trailing ,.
I suggest using
^[1-9]\d{8}(?:,[1-9]\d{8}){2,19}$
See the regex demo
Details:
^ - start of string
[1-9] - the first digit of the 9-digit number cannot be zero, 1 to 9 only
\d{8} - the 8 remaining digits of the number
(?:,[1-9]\d{8}){2,19} - 2 to 19 (in total, 3 to 20) occurrences of
, - comma
[1-9]\d{8}){2,19} - see above
$ - end of string.
preg_match('=^[1-9][0-9,]*$=', $x); should do it, unless you're saying the number must be 9 digits long (with optional commas? or are the commas required?), in which case try something like preg_match('=^[1-9][0-9]{2},?[0-9]{3},?[0-9]{3}$=', $x);

PHP Pattern Validation

I'm having a bit of trouble getting my pattern to validate the string entry correctly. The PHP portion of this assignment is working correctly, so I won't include that here as to make this easier to read. Can someone tell me why this pattern isn't matching what I'm trying to do?
This pattern has these validation requirements:
Should first have 3-6 lowercase letters
This is immediately followed by either a hyphen or a space
Followed by 1-3 digits
$codecheck = '/^([[:lower:]]{3,6}-)|([[:lower:]]{3,6} ?)\d{1,3}$/';
Currently this catches most of the requirements, but it only seems to validate the minimum character requirements - and doesn't return false when more than 6 or 3 characters (respectively) are entered.
Thanks in advance for any assistance!
The problem here lies in how you group the alternatives. Right now, the regex matches a string that
^([[:lower:]]{3,6}-) - starts with 3-6 lowercase letters followed with a hyphen
| - or
([[:lower:]]{3,6} ?)\d{1,3}$ - ends with 3-6 lowercase letters followed with an optional space and followed with 1-3 digits.
In fact, you can get rid of the alternation altogether:
$codecheck = '/^\p{Ll}{3,6}[- ]\d{1,3}$/';
See the regex demo
Explanation:
^ - start of string
\p{Ll}{3,6} - 3-6 lowercase letters
[- ] - a positive character class matching one character, either a hyphen or a space
\d{1,3} - 1-3 digits
$ - end of string
You need to delimit the scope of the | operator in the middle of your regex.
As it is now:
the right-side argument of that OR runs up until the very end of your regex, even including the $. So the digits, nor the end-of-string condition do not apply for the left side of the |.
the left-side argument of the OR starts with ^, and only applies to the left side.
That is why you get a match when you supply 7 lowercase characters. The first character is ignored, and the rest matches with the right-side of the regex pattern.

Categories