How can I check if the input is C12345 ( Capital letter "C" followed by 5 numbers (total 6 digits)
I have this code
$val = "/^\d\d\d\d\d\d$/";
if (! preg_match($val, $source)) {
$error_msg .= "<p>your source must be 6 digits long</p>";
}
$secval = "/^\"C\"\d\d\d\d\d$/";
if (! preg_match($secval, $source)) {
$error_msg .= "<p>source must be 6 digits long and start with an "S"</p>";
}
try this
^[A-Z]\d{5}$
Single letter, A through Z followed by 5 digits
If the first letter must be C, use
^C\d{5}$
Use this regex:
^C\d{5}$
Debuggex Demo
If you want any other character, use the character set [A-Z], which means, any character from capital A to Z
^[A-Z]\d{5}$
Related
I want to allow lowercase characters and numbers in username field.
But with following conditions...
Only numbers as username NOT allowed (e.g. only mobile number)
Only lowercase characters allowed (e.g. without any number in username)
Lowercase characters + numbers allowed (e.g. combination of lowercase and numbers)
Minimum length 8 characters required
Maximum length 20 characters allowed
What php regex will do it ?
I tried with following, but it forces lowercase + numbers. Only lowercase username not allowing.
$username_pattern = '/^(?=.*[a-z])(?=.*[a-z])(?=.*\d)[a-z0-9]{8,20}$/';
I want only lowercase and/or lowercase+numbers ( min 8 and max 20 ) in username
Help appreciated.
You can simplify it to not allowing only digits
^(?!\d*$)[a-z0-9]{8,20}$
Explanation
^ Start of string
(?!\d*$) Negative lookahead, assert not only digits till end of string
[a-z0-9]{8,20} Match 8-20 times a char a-z or a digit 0-9
$ End of string
Regex demo | Php demo
$username_pattern = '/^(?!\d*$)[a-z0-9]{8,20}$/';
$userNames = [
"1a3b5678",
"1a3b5678abcd",
"12345678",
"1a3b5678abcddddddddddddddddddddddddddddddd",
"1a3B5678",
"a1"
];
foreach ($userNames as $userName) {
if (preg_match($username_pattern, $userName)) {
echo "Match - $userName" . PHP_EOL;
} else {
echo "No match - $userName" . PHP_EOL;
}
}
Output
Match - 1a3b5678
Match - 1a3b5678abcd
No match - 12345678
No match - 1a3b5678abcddddddddddddddddddddddddddddddd
No match - 1a3B5678
No match - a1
I'm doing validation on Australian DVA numbers, the rules are:
String length should be 8 or 9
First char should be N, V, Q, W, S or T
The next part should be letters or space and can have up to 3 characters
Next part should be number and can have up to 6 number
If the string length is 9 then last char is a letter, if 8 then it must be a number // This is the tricky part
Here is my current attempt and it's working fine:
if (strlen($value) == 9 && preg_match("/^[NVQWST][A-Z\s]{1,3}[0-9]{1,6}[A-Z]$/", $value)) {
return true;
}
if (strlen($value) == 8 && preg_match("/^[NVQWST][A-Z\s]{1,3}[0-9]{1,6}$/", $value)) {
return true;
}
return false;
My question: Is there any way that I can combine these conditions in 1 regex check?
You can use
^(?=.{8,9}$)[NVQWST][A-Z\s]{1,3}[0-9]{1,6}(?:(?<=^.{8})[A-Z])?$
See the regex demo.
Details
^ - start of a string
(?=.{8,9}$) - the string should contain 8 or 9 chars (other than line break chars, but the pattern won't match them)
[NVQWST] - N, V, Q, W, S or T
[A-Z\s]{1,3} - one, two or three uppercase letters or whitespace
[0-9]{1,6} - one to six digits
(?:(?<=^.{8})[A-Z])? - an optional occurrence of an uppercase ASCII letter if it is the ninth character in a string
$ - end of string.
Based on rules and details pulled from these links:
https://www.ppaonline.com.au/wp-content/uploads/2019/05/DVA-number-format-factsheet.pdf
https://meteor.aihw.gov.au/content/339127
I've crafted a comprehensive and strict regex to validate Australian DVA numbers.
$regex = <<<REGEX
/
^
([NVQWST])
(?|
([ ANPVX])(\d{1,6})
|(
BG
|CN
|ET
|F[RW]
|G[RW]
|I[QTV]
|JA
|K[MO]
|MO
|N[FGKX]
|P[KOX]
|R[DMU]
|S[AELMORS]
|U[BS]
|YU
)(\d{1,5})
|(
(?:A(?:FG|GX|LX|R[GX])
|B(?:A[GL]|CG|G[GKX]|RX|U[GRX])
|C(?:AM|CG|HX|IX|LK|N[KSX]|ON|YP|Z[GX])
|D(?:EG|N[KX])
|E(?:G[GXY]|SX|T[KX])
|F(?:I[JX]|R[GKX])
|G(?:HA|R[EGKX])
|H(?:K[SX]|L[GKX]|UX)
|I(?:DA|ND|SR|T[GKX])
|K(?:OS|SH|UG|YA)
|L(?:AX|BX|XK)
|M(?:A[LRU]|LS|OG|TX|WI)
|N(?:BA|CG|GR|IG|RD|S[MSW]|W[GKX])
|OMG
|P(?:A[DGLMX]|C[AGRV]|H[KSX]|L[GX]|MS|S[MW]|WO)
|QAG
|R(?:DX|U[GX])
|S(?:A[GX]|CG|EG|IN|PG|UD|W[KP]|Y[GRX])
|T(?:H[KS]|R[GK]|ZA)
|U(?:AG|RX|S[GKSX])
|V(?:EX|NS)
|Y(?:EM|GX)
|ZIM
)
)(\d{1,4})
)
([A-Z]?)
$
/x
REGEX;
The first character signifies the state/territory.
N = New South Wales (includes Austalian Capital Territory)
V = Victoria
Q = Queensland
W = Western Australia
S = South Australia (includes Northern Territory)
T = Tasmania
My pattern intentionally uses "branch reset" capture groups so that the match array can be easily used to pad the inner "file number" with leading digits when desired.
Here is a demo with sample DVA strings, a preg_match() call, and zero-padding of the file number to represent full length format.
If your application requires the DVAs to be zero padded to 8 or 9 characters, then this is a tighter pattern to enforce that.
Yes, I did this all on my phone.
No, I didn't type it all out maually.
I scraped the one webpage and used regex to format the content into array syntax for my lookup array.
Then I compacted the war code abbreviations into groups and character classes.
I need to match following pattern using php Regular expression but it doesn't give expected out come.
ex:-
need to match pattern
5555 5545 9930
$id = "4567 3423 4567";
$regex = "/^[1-9]\d{4} \d{4} \d{4}$/";
if (preg_match($regex, $id)) {
// Indeed, the expression "^[2-9]\d{2} \d{3} \d{4}$" matches the date string
echo "Found a match!";
}else {
// If preg_match() returns false, then the regex does not
// match the string
echo "The regex pattern does not match. :(";
}
If you want to match: 4 non-zero digits + space + 4 digits + space + 4 digits
^([1-9]){4} \d{4} \d{4}$ should do the trick
I think the safest way to modify the existing regex is by adding an alternative to the first [2-9]\d{2} \d{3}:
^(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) \d{4}$
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) - one of the alternatives:
[2-9]\d{2} \d{3} - a digit from 2 to 9, any 2 digits, a space and 3 digits
| - or
\d{4} \d{4} - 4 digits, space, 4 digits (for the new string types)
- a space
\d{4} - 4 digits
$ - end of string.
See the PHP demo:
$id = "4567 3423 4567";
$regex = "/^(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) \d{4}$/";
if (preg_match($regex, $id)) {
echo "Found a match!";
} else {
echo "The regex pattern does not match. :(";
}
Simple edit of your regular expression will be
^[1-9]\d{3} \d{4} \d{4}
I want to create a regular expression in PHP, which will allow to user to enter a phone number in either of the formats below.
345-234 898
345 234-898
235-123-456
548 812 346
The minimum length of number should be 7 and maximum length should be 12.
The problem is that, the regular expression doesn't care about the minimum and maximum length. I don't know what is the problem in it. Please help me to solve it. Here is the regular expression.
if (preg_match("/^([0-9]+((\s?|-?)[0-9]+)*){7,12}$/", $string)) {
echo "ok";
} else {
echo "not ok";
}
Thanks for reading my question. I will wait for responses.
You should use the start (^) and the end ($) sign on your pattern
$subject = "123456789";
$pattern = '/^[0-9]{7,9}$/i';
if(preg_match($pattern, $subject)){
echo 'matched';
}else{
echo 'not matched';
}
You can use preg_replace to strip out non-digit symbols and check length of resulting string.
$onlyDigits = preg_replace('/\\D/', '', $string);
$length = strlen($onlyDigits);
if ($length < 7 OR $length > 12)
echo "not ok";
else
echo "ok";
Simply do this:
if (preg_match("/^\d{3}[ -]\d{3}[ -]\d{3}$/", $string)) {
Here \d means any digits from 0-9. Also [ -] means either a space or a hyphen
You can check the length with a lookahead assertion (?=...) at the begining of the pattern:
/^(?=.{7,12}$)[0-9]+(?:[\s-]?[0-9]+)*$/
Breaking down your original regex, it can read like the following:
^ # start of input
(
[0-9]+ # any number, 1 or more times
(
(\s?|-?) # a space, or a dash.. maybe
[0-9]+ # any number, 1 or more times
)* # repeat group 0 or more times
)
{7,12} # repeat full group 7 to 12 times
$ # end of input
So, basically, you're allowing "any number, 1 or more times" followed by a group of "any number 1 or more times, 0 or more times" repeat "7 to 12 times" - which kind of kills your length check.
You could take a more restricted approach and write out each individual number block:
(
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
)
Simplified:
if (preg_match('/^(\d{3}(?:[ ]+|-)?\d{3}(?:[ ]+|-)?\d{3})$/', $string)) {
If you want to restrict the separators to be only a single space or a hyphen, you can update the regex to use [ -] instead of (?:[ ]+|-); if you want this to be "optional" (i.e. there can be no separator between number groups), add in a ? to the end of each.
if (preg_match('/^(\d{3}[ -]\d{3}[ -]\d{3})$/', $string)) {
may it help you out.
Validator::extend('price', function ($attribute, $value, $args) {
return preg_match('/^\d{0,8}(\.\d{1,2})?$/', $value);
});
I am trying to write an if statement in php with preg_match to say allow 4 numbers and then a dot and then 2 numbers...
This is what I have....
$string = "10000.000";
if (preg_match('[/^\d{0,4}(\.\d{1,2})?$/]', $string)){
return TRUE;
} else {
return FALSE;
}
is my preg_match code wrong?
This should work as you want it!
^[0-9]{4}\.[0-9]{2}$
^ start of string,
[0-9] a number from 0 to 9,
{4} 4x times,
\. a dot,
$ end of the string