Ask a group of lowercase letters (A to z), underscores (_), and hyphens (-) that contain English letters and numbers, can contain one or more members in Group.
The number 0-9 is a group called B, which can contain one or more members in Group B.
Pure A combination or pure B combination, allowed, such as B, aaa, a __, a-B, 456,000, 0 allowed
AB combination, or BA combination, allowed, for example a4, 5b, ag_d87, 7588 _ faae
ABA combination, or BAB combination, not allowed, such as aa7_f, 3322a_4234, as7gwe9fwef, 28_957a8ff927
Can I use a regular expression or a few regular expressions?
Reply to discussion (solution)
// Only AB or BA $ str = '28 _ 957a8ff927 'is allowed '; if (preg_match ('# ^ ([a-z _-] + \ d + | \ d + [a-z _-] +) $ #', $ str, $ m) {print_r ($ m);} else {echo 'failed';}/* The preg_match ('#(?
A = [a-z _-]
B = [0-9]
P = A * B * | B * *
$ Pattern = '/[a-z _-] * \ d * | \ d * [a-z _-] */'
A = [a-z _-]
B = [0-9]
P = A * B * | B * *
$ Pattern = '/[a-z _-] * \ d * | \ d * [a-z _-] */'
It doesn't seem to work .......
I wrote this previously/^ ([0-9] * [a-z] * | [a-z] * [0-9] *) $ /,,, it doesn't seem to work either .... When there is an a4 location
// Only AB or BA $ str = '28 _ 957a8ff927 'is allowed '; if (preg_match ('# ^ ([a-z _-] + \ d + | \ d + [a-z _-] +) $ #', $ str, $ m) {print_r ($ m);} else {echo 'failed';}/* The preg_match ('#(?
It doesn't seem to work. Check abc or 123
$pattern = '/(^[a-z_-]*\d*$)|(^\d*[a-z_-]*$)/';$data = array( 'b', 'aaa', 'a__', 'a-b', '456', '000', '0', 'a4', '5b', 'ag_d87', '7588__faae', 'aa7_f', '3322a_4234', 'as7gwe9fwef', '28_957a8ff927',);foreach($data as $str) printf("%d %s\n", preg_match($pattern, $str), $str); 1 b1 aaa1 a__1 a-b1 4561 0001 01 a41 5b1 ag_d871 7588__faae0 aa7_f0 3322a_42340 as7gwe9fwef0 28_957a8ff927