Hammad Tariq
Hammad Tariq

Reputation: 1523

how to match a specific integer pattern in PHP using reg expressions?

I am looking for a reg exp to match 1111111/11 pattern, all numbers are integers.

I will be grateful if anyone can please help? I am not that good in regular expressions.

Upvotes: 0

Views: 230

Answers (2)

Toto
Toto

Reputation: 91415

if your input string is like abc1234567/89def you could use:

preg_match('~\D\d{7}/\d{2}\D~', $input);

Upvotes: 0

Matt Huggins
Matt Huggins

Reputation: 83279

Assuming you're trying to match 7 digits, a slash, and then 2 more digits, you'll want a regex pattern like the following:

/^[0-9]{7}\/[0-9]{2}$/

In PHP, your overall code will look something like this:

if (0 !== preg_match('/^[0-9]{7}\\/[0-9]{2}$/', $testString)) {
    // success!
}

Upvotes: 1

Related Questions