1

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!

3
  • BTW, it seems you may use /^fl-(IV|I{1,3}|br-II)-\d*\W?$/i. The second alternative is almost identical to the first one, and the last \W prevents the last item to match. See regex101.com/r/34eka2/1. Commented Nov 14, 2018 at 8:57
  • @WiktorStribiżew Thanks for improved expression, but still same error message The lic no format is invalid Commented Nov 14, 2018 at 9:00
  • @WiktorStribiżew /^fl-(IV|I{1,3}|br-II)-\d*\W?$/i this one worked, can you please add answer so that I can accept. Thanks! Commented Nov 14, 2018 at 9:03

1 Answer 1

1

Your regex does not match the last item in your list of expected matches. You may combine the second alternative with the first one and make the last \W optional:

/^fl-(IV|I{1,3}|br-II)-\d*\W?$/i

See the regex demo

Details

  • ^ - start of string
  • fl- - a fl_ text
  • (IV|I{1,3}|br-II) - a capturing group (add ?: after ( to make it non-capturing) matching IV, one to three Is or br-II
  • - - a hyphen
  • \d* - 0+ digits
  • \W? - an optional non-word char
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.