Ask a regular expression that contains English letters and numbers.

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.