简体   繁体   中英

How to match two digits in pregmatch?

I have return a preg_match to check whether starting digits were 9477 or not and it works correctly. But now I want add another condition to add 9477 or 9476 should be valid.

Here is the condition:

  1. should contain 11 digits
  2. should starts with 9477 or 9476

Here is my code:

preg_match('/^9477\d{7}$/',$Mnumber)

Use an alternation between the two numbers:

preg_match('/^947(?:7|6)\d{7}$/',$Mnumber)

(?:7|6) is a non capture group that matches digit 7 or 6 . A non capture group is much more efficient than a capture group.

You can do also:

preg_match('/^947[76]\d{7}$/',$Mnumber);

[67] is a character class that matches digit 7 or 6

在[]中使用分组

echo preg_match('/^947[76]\d{7}$/',$Mnumber);

Just use (9477|9476)

echo preg_match('/^(9477|9476)\d{7}$/',$Mnumber);

You can also use:

  1. /^947(7|6)\\d{7}$/
  2. /^947[76]\\d{7}$/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM