[FIXED] Need Help with Regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

[FIXED] Need Help with Regex

Post by Pyrite »

In the game Urban Terror, you can issue a rcon command called status which returns something like:

[text]
map: ut4_algiers
num score ping name lastmsg address qport rate
--- ----- ---- --------------- ------- --------------------- ----- -----
4 1 45 {&}TheBrew 0 74.97.237.134:27960 13024 8000
5 1 103 [Vzla]el_enano 0 201.211.207.178:27960 16340 8000
6 0 50 Dys-Fuction 0 74.178.223.228:50113 6715 25000
7 1 77 /eVo/Regal 0 24.58.209.191:55008 62323 25000
8 0 89 Calmartin65 0 173.24.175.167:27960 25564 8000
9 0 900 victoor_1998 50 88.16.65.147:27960 7213 8000
10 0 20 Pyrite[1up] 0 70.254.40.137:27960 59893 8000
11 4 160 AO2|Abruti. 0 85.69.3.61:27960 50160 8000
12 5 100 xil-rev 0 68.144.28.14:27960 33288 24999
[/text]

Each line of that output is ending with a newline, so I'm exploding the output based on the "\n" character into an array of lines, then calling preg_match_all on each line.

Code: Select all

preg_match_all("/^\s*(\d+)\s*(-{0,1}\d+)\s*(\d+)(.*?)(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $line, $out);
The regex should separate each column into the $out array.

I'm seeing a reoccurring problem with it having trouble with player names that are only 3 characters long. Any idea why the (.*?) would cause that?
Last edited by Pyrite on Mon Mar 29, 2010 7:36 pm, edited 1 time in total.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Re: Need Help with Regex

Post by Pyrite »

Hmm, looks like I fixed it. I just added some \s* around the (.*?) and that seems to have fixed it.

Code: Select all

preg_match_all("/^\s*(\d+)\s*(-{0,1}\d+|CNCT|ZMBI)\s*(\d+)\s*(.*?)\s*(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $line, $out);
Post Reply