Regex to allow multiple value

I have got the following regex

^[a-z-A-Z]{4}[0-9]{2}|^[a-z-A-Z]{2}[0-9]{4}$

This allows things like YJ1313 or JDTZ11. What I am looking for is to update this regex to allow multi values separated by coma, say YJ1313,JDTZ11,XX1111,AABB88,YY1111,RRTT11

Where is this csv data coming from in the first place?

I don’t understand your question.

(a|b)(,a|,b)*

^(a-z-A-Z]{4}[0-9]{2}|[a-z-A-Z]{2}[0-9]{4})(,[a-z-A-Z]{4}[0-9]{2}|,[a-z-A-Z]{2}[0-9]{4})*$

It can probably be done better than that, but that should work.

1 Like

Your data with the commas in it, where is it coming from? A database, a text file?

It appears to be working

User’s input in form. Those are Chilean license plates and the user so far could enter just one but they want being able to enter several.
The format is either 2 letters and 4 digits or 4 letters and 2 digits

I would split it first and check them one by one

$errors = [];

// $input should contain the input by the user
// use 'trim' to ignore any spaces around commas
$licensplates = array_map('trim', explode(',', $input));
foreach ($licenseplates as $licenseplate) {
    if (!preg_match('^[a-z-A-Z]{4}[0-9]{2}|^[a-z-A-Z]{2}[0-9]{4}$', $licenseplate)) {
        $errors[] = sprintf('Licence plate "%s" is invalid', $licenseplate);
    }
}

// do something nice with the errors please
// like rendering them in the form
// this is just an example
if (!empty($errors)) {
   echo 'Computer says no!';
   var_dump($errors);
   exit;
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.