I want to validate the string containing characters A
, B
. X
& Y
. The character A & B must follow by the number but X, Y should not be.
// correct
"A11X",
"A45YA1X",
"A1XXA999YYA1",
// Not correct
"A1A1AAX12",
"1A1Y32A1",
"CA1A1",
"A1C1",
I am using following regex command.
$flags = PREG_SET_ORDER;
preg_match_all('/A|B(\d+)|.+/i', trim($command), $operations, $flags);
If the string is correct, it must return true otherwise it should return error message.
You may use this regex:
^(?:[AB]\d+|[XY])+$
This regex uses a non-capturing group that matches letter A
or B
followed by 1+ digits or using alternation matches letter X
or Y
.
This group is repeated 1 or more times to allow repetitions of this sub-pattern in entire string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.