1

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

Synergy
  • 13
  • 3
  • 1
    Please give more details about what the expression should catch, and what it shouldn't catch – Pekka Dec 26 '10 at 13:02
  • well it needs to make sure the format is something alike STEAM_0:1:1928519 so it'll have to check if its got STEAM_[number 0 to 5]:[0 or 1]:[any number here] – Synergy Dec 26 '10 at 13:03
  • Will the data always be in that format? Do you need to detect a string of that format inside arbitrary data, or do you need a function to validate a string of that format? – Pekka Dec 26 '10 at 13:06
  • I need to make sure STEAM_ is the starter, I've edited the main post to show an Valid and Invalid entry. I do not need to detect a certain number in the string, I just need to make sure its a correct entry so the script can continue going along. – Synergy Dec 26 '10 at 13:07
  • What did you try so far? Or do you just expect us to write a regex for you, since you have no intention to look up regex syntax in the manual? If you throw it into your code, but have no clue how it works, it will become unmaintable. So, either learn regexps, or resort to string functions (which would work for this specific case). – mario Dec 26 '10 at 13:10
  • I expect some help with this little confusing issue, I'm not very good at regex as I can only find one word or letter in a string, so I'm disappointed to say that I was expecting someone to write me a small one to detect if the string was valid or invalid. – Synergy Dec 26 '10 at 13:13

4 Answers4

3
preg_match('/^STEAM_[0-5]:[01]:[0-9]+$/', $input)
phihag
  • 278,196
  • 72
  • 453
  • 469
2
preg_match('/^STEAM_[0-5]:[0-1]:[0-9]+$/', $x)
Vojta
  • 23,061
  • 5
  • 49
  • 46
1

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

d-live
  • 7,926
  • 3
  • 22
  • 16
0
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
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66