user3217871
user3217871

Reputation: 41

PHP regular expression to match exact string

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

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

In 2nd part i.e. 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)

Upvotes: 3

Views: 6660

Answers (2)

brandonscript
brandonscript

Reputation: 72855

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

Upvotes: 1

Barmar
Barmar

Reputation: 780818

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

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

Upvotes: 5

Related Questions