简体   繁体   中英

PHP regular expression to match exact string

I have string like "ABC 1000", "ABC 1", "ABC 100".

In above string example, first 3 character ie ABC is fixed every time and then digits, digits can be long upto N numbers.

In 2nd part ie after "ABC " it should always be numberic value, no alphabet, no special symbol.

So, how can I manage with regular expression. Please help.

I have tried with following but failed ..

$var="ABC 100";

preg_match("/^INR /[0-9]+/", $var)

You have an extra / in your regular expression. It should be:

preg_match('/^ABC \d+/', $var);

You could use this:

^[A-Za-z]{3} *\d+$

http://regex101.com/r/gE4mS4

$var="ABC 100";
preg_match("/^[A-Za-z]{3} *\d+$/", $var)
  • 3 letters (case insensitive)
  • 0 or more space
  • 1 or more digits

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