I am new to Regular Expression and tried to write a line of code to match
- strings should NOT start with digits. ( OK )
- strings should have only alpha, num, dash and underscore in it. ( OK )
- strings length should be between 5 and 25 ( FAIL )
rule 1 and 2 works correctly but rule 3 doesn't.
any help to fix it?
This is my code :
$arr = [
'-foobar',
'foobar',
'32-xx',
'xx23',
'A2l',
'2aAA',
'_a2d',
'-A22',
'-34x',
'2--a',
'a--a-'
];
foreach( $arr as $a ){
echo check( $a );
}
function check($string){
if (preg_match("/^[a-zA-Z]+([a-zA-Z0-9_-]?){5,25}$/", $string)) {
return "$string ---------------> match was found.<br />";
} else {
return "$string ---------------> match was not found.<br />";
}
}