Synergy
Synergy

Reputation: 13

Small number detecting regex

I need it to detect something like

STEAM_numberhere 0 to 5:numberhere only 0 or 1:any number here

PHP preg_match

Thanks for your help!

STEAM_X:Y:Z

X has to be 0, 1, 2, 3, 4 or 5

Y has to be either 0 or 1

Z can be any number, just can't contain any text.

Valid: STEAM_0:1:1958281

Invalid: STEAM_9:4:1912tg

Upvotes: 1

Views: 115

Answers (4)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

preg_match("#STEAM_[0-5]{1}:[0-1]{1}:([0-9]+)#", $string, $matches);

Something like this, haven't tested it.

Tested:

$string = "STEAM_0:1:1928519 STEAM_1:5:19a28519 STEAM_18:1:1928519x";

preg_match("#STEAM_[0-5]{1}:[0-1]{1}:([0-9]+)#", $string, $matches);

echo($matches[0]); //STEAM_0:1:1928519

Upvotes: 0

Vojta
Vojta

Reputation: 23051

preg_match('/^STEAM_[0-5]:[0-1]:[0-9]+$/', $x)

Upvotes: 2

d-live
d-live

Reputation: 8036

How about STEAM_[0-5]:[0-1]:[0-9]+

Upvotes: 1

phihag
phihag

Reputation: 288298

preg_match('/^STEAM_[0-5]:[01]:[0-9]+$/', $input)

Upvotes: 3

Related Questions