What's the regex for validating input for this
Below 3 line are valid
PROJ9450
PROJ9400-PROJ9401-PROJ9402 ..... PROJ{n}
PROJ9400_1-PROJ9400_2-PROJ9401_1-PROJ9402_1-PROJ9408 ... PROJ{n}_{n}
Below lines are Invalid strings
PROJ450
PRO1223
PROJ9400a-PROJ9401-PROJ9400-PROJ1929-1-PROJ1929
PROJ9400_1-PROJ9400_2-PROJ9401_1-PROJ9402_1-PROJs453 ... PROJ{n}_{n}
I tried this
if( preg_match('/(PROJ)[0-9]{4}(-|_)?[0-9]+)/', $input) )
{
}
I can split and can validate like something like below , but I want to do this by single regex
foreach(explode('-',$input) as $e)
{
if( !preg_match('/(PROJ)[0-9]{4}(-|_)?[0-9]+)/', $e) )
{
return 'Invalid Input';
}
}
Input can be just prefixed by PROJ and 4 digit number
PROJ9450
OR
Input can be prefixed by PROJ and 4 digit number - prefixed by PROJ and
4 digit number like this upto n
PROJ9400-PROJ9401-PROJ9402 ..... PROJ{n}
OR
Input can be prefixed by PROJ and 4 digit number undescore digit -
prefixed by PROJ and 4 digit number underscore digit like this upto
n
PROJ9400_1-PROJ9400_2-PROJ9401_1-PROJ9402_1 ... PROJ{n}_{n}
You need to match the block starting with PROJ and followed with 4 digits (that are optionally followed with - or _ and 1+ digits) repeatedly.
Use
/^(PROJ[0-9]{4}(?:[-_][0-9]+)?)(?:-(?1))*$/
See the regex demo
Details:
^ - start of string anchor
(PROJ[0-9]{4}(?:[-_][0-9]+)?) - Group 1 (that will be later recursed with (?1) recursion construct) capturing:
PROJ - a literal char sequence
[0-9]{4} - 4 digits
(?:[-_][0-9]+)? - an optional (1 or 0 occurrences) of
[-_] - a character class matching - or _
[0-9]+ - 1 or more digits
(?:-(?1))* - zero or more occurrences of - followed with Group 1 subpattern up to...
$ - end of string (better replace it with \z anchor that matches the very end of the string).
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 validate a number with those criterias :
Can be float or integer
Scale 4
Precision 2
I tried like this :
pattern="/^[-+]?[0-9]\d*(\.\d+)?$/i",
Some examples :
valid : 2; -1; 0.4; 0.12; 1928; 1827.78; -182.4
invalid : 10000; 0.345; 89374.5;
Thx in advance.
You may use
'/^[-+]?(?:[1-9]\d{0,3}|0)(?:\.\d{1,2})?$/'
See the regex demo.
Details
^ - start of string
[-+]? - either - or +
(?:[1-9]\d{0,3}|0) - a non-capturing group matching either
[1-9]\d{0,3} - a digit from 1 to 9 (non-zero) and any 0 to 3 digits
| - or
0 - a zero
(?:\.\d{1,2})? - an optional non-capturing group matching 1 or 0 occurrences of
\. - a dot
\d{1,2} - 1 or 2 digits
$ - end of string.
I would like to create a preg_match function to validate my username, my code below not working perfectly, especially on Must contain at least 4 letter lowercase rules and number not more than 4 character and place behind letter
if (preg_match('/^[a-z0-9]{4,12}/', $_POST['username']))
Here are my username rules that I want to work :
Only contain letter and numbers but number is not reqired
Must contain at least 4 letter lowercase
Number not more than 4 character and place behind letter
Must be 4-12 characters
Thank you for any help you can offer.
You match these criteria, maybe this will be an option:
^[a-z](?=(?:[a-z]*\d){0,4}(?![a-z]*\d))(?=[a-z\d]{3,11}$)[a-z\d]+$
This will match
From the beginning of the string ^
Match a lowercase character [a-z]
A positive lookahead (?= which asserts that what follows is
A non capturing group (?:
Which will match a lowercase character zero or more times followed by a digit [a-z]*\d
Close non capturing group and repeat that from 0 to 4 times ){0,4}
A negative lookahead (?! Which asserts that what follows is not
A lowercase character zero or more times followed by a digit [a-z\d]*
Close negative lookahead )
Close positive lookahead )
Positive lookahead (?= which asserts that what follows is
Match a lowercase character or a digit from 3 till 11 times till the end of the string (?=[a-z\d]{3,11}$)
Close positive lookahead )
Match a lowercase character or a digit till the end of the string [a-z\d]+$
Out php example
Regex: ^[a-z]{4,8}[0-9]{0,4}$|^[a-z]{4,12}$
Details:
^ Asserts position at start of a line
$ Asserts position at the end of a line
[] Match a single character present in the list
{n,m} Matches between n and m times
| Or
PHP code:
$strings=['testtesttest', 'testtesttestr', 'test12345', 'testtest1234', 'testte123432'];
foreach($strings as $string){
$match = preg_match('~^[a-z]{4,8}[0-9]{0,4}$|^[a-z]{4,12}$~', $string);
echo ($string . ' => len: (' . strlen($string) . ') ' .($match ? 'true' : 'false')."\n");
}
Output:
testtesttest => len: (12) true
testtesttestr => len: (13) false
test12345 => len: (9) false
testtest1234 => len: (12) true
testte123432 => len: (12) false
Code demo
I need split address: Main Str. 202-52 into
street=Main Str.
house No.=202
room No.=52
I tried to use this:
$data['address'] = "Main Str. 202-52";
$data['street'] = explode(" ", $data['address']);
$data['building'] = explode("-", $data['street'][0]);
It is working when street name one word. How split address where street name have several words.
I tried $data['street'] = preg_split('/[0-9]/', $data['address']);But getting only street name...
You may use a regular expression like
/^(.*)\s(\d+)\W+(\d+)$/
if you need all up to the last whitespace into group 1, the next digits into Group 2 and the last digits into Group 3. \W+ matches 1+ chars other than word chars, so it matches - and more. If you have a - there, just use the hyphen instead of \W+.
See the regex demo and a PHP demo:
$s = "Main Str. 202-52";
if (preg_match('~^(.*)\s(\d+)\W+(\d+)$~', $s, $m)) {
echo $m[1] . "\n"; // Main Str.
echo $m[2] . "\n"; // 202
echo $m[3]; // 52
}
Pattern details:
^ - start of string
(.*) - Group 1 capturing any 0+ chars other than line break chars as many as possible up to the last....
\s - whitespace, followed with...
(\d+) - Group 2: one or more digits
\W+ - 1+ non-word chars
(\d+) - Group 3: one or more digits
$ - end of string.
Also, note that in case the last part can be optional, wrap the \W+(\d+) with an optional capturing group (i.e. (?:...)?, (?:\W+(\d+))?).