Home > front end >  Regex that accepts only three specific letters, a hyphen , 6 or 9, then numbers
Regex that accepts only three specific letters, a hyphen , 6 or 9, then numbers

Time:12-31

It must start with SEQ or ABC or MHT followed by '-' then 6 or 9 then followed by any number of digits.

example: SEQ-900000 (should pass)

$value = $data['firstname'];
// ^$ = anchors, [a-zA-Z ] = letters/spaces, {1,30} = 1-30 characters
$format = "/^[a-zA-Z ]{1,30}$/";
// If value does NOT match the format then it is invalid
if (!preg_match($format, $value)) {
    $feedback['firstname'] = 'Server feedback: Only 1-30 letters/spaces are permitted';
    $valid = false;
}

CodePudding user response:

This java method will work for your situation. (d stands for one or more decimals)

    public static boolean string_to_check(String string){
    return string.matches("(SEQ|ABC|MHT)-[69]\\d ");
    }

CodePudding user response:

Use a start-of-line anchor (^), then a non-capturing group and pipe-separated branches to match your abbreviation, then a literal hyphen, then a 6 or a 9, then zero or more digits, then an end-of-string anchor ($).

Code: (Demo)

$string = 'SEQ-900000';

var_export(
    (bool)preg_match(
        '~^(?:SEQ|ABC|MHT)-[69]\d*$~',
        $string
    )
);
// true
  • Related