I am trying to validate entered string against regex expression, this working fine on websites like regexr and regex101 but it is always showing error on in laravel.
Regex should match with following strings:
FL-IV-1234
FL-III-1234
FL-II-56789
FL-I-1234334
FL-BR-II-53440
fl-iv-8484
fl-iii-84894
fl-ii-94
fl-i-334
Expression:
/(fl)-(IV-|I{1,3}-)(\d*\W)|((fl)-(br)-II-\d*\W)/i
Code:
$pattern = '/(fl)-(IV-|I{1,3}-)(\d*\W)|((fl)-(br)-II-\d*\W)/i';
$request->validate([
'lic_no' => array('required', 'regex:'.$pattern),
]);
Also tried without variable:
$request->validate([
'lic_no' => array('required', 'regex:/(fl)-(IV-|I{1,3}-)(\d*\W)|((fl)-(br)-II-\d*\W)/'),
]);
Error message:
The lic no format is invalid.
Please suggest, Thanks!
/^fl-(IV|I{1,3}|br-II)-\d*\W?$/i. The second alternative is almost identical to the first one, and the last\Wprevents the last item to match. See regex101.com/r/34eka2/1.The lic no format is invalid/^fl-(IV|I{1,3}|br-II)-\d*\W?$/ithis one worked, can you please add answer so that I can accept. Thanks!