So basically I'm developing a PHP RCon tool for use in the CoD series of games. I am limited to work with what comes out of the server. Ex: $data is the responce packet(s) from the query. That can not be changed. And the amount of whitespace between entries are subject to change as well, though there will always be at least one space. The problem that I'm having is getting player names with a space in them.
Okay, that makes it a bit clearer.
Now about the user name. Before coding (or crafting a regex), you should have a clear picture of what a user name "could" be. Your sole example ("Payer ??") is probably not the format the 'name' will be each time. So a couple of questions:
1 - is there always one space in the user name, or can there be no spaces or more than one space?
2 - does the user name always begin with "Player"?
3 - if question 2 is answered with a "no", what are the valid characters a user name can consist of?
The player names can consist of about any ASCII character(there are more allowed than not) with an array of no spaces, one space, or multiple spaces(or even multiple spaces in a row).
EDIT: NVM, I have a solution. I'm not sure if I should post it since it was on another forum.
Your regex has a lot of greedy stars which could result in catastrophic backtracking if any line is not well formed. You want to use a + rather than a * anyway, because you know something must be in each field. Also, if the data file has only spaces between the fields, then use only spaces in the regex and don't use the \s because it can match the end of lines. To get the job done efficiently, liberally use possessive quantifiers on everything but the name, which appears to be the only "variable" element which may contain multiple spaces. Also, a negative lookbehind after the "name" capture prevents it from grabbing any trailing spaces. And finally, adding optional minus sign if front of numeric fields makes sense. Here is a regex that seems to do the trick quite nicely...
It only shows correct entries since I am receiving the packets over UDP which puts them out of order more often than not. It needed to be able to ONLY show entries which aren't malformed and exclude everything else.
It only shows correct entries since I am receiving the packets over UDP which puts them out of order more often than not. It needed to be able to ONLY show entries which aren't malformed and exclude everything else.
This one is better than your first but still grabs trailing spaces into the captured name field. And the negative lookahead within the name capture is effectively doing nothing at all. (It appears to be trying (but failing) to avoid occurrences of 2 or more spaces within a name.)
If you don't want trailing spaces in your name field, you could remove the useless negative lookahead within the name capture and add a negative lookbehind right after like so:
The regex I presented in my previous post does match only well formed entries - and it fails to match malformed lines very quickly i.e. it is not susceptible to catastrophic backtracking. It correctly captures a name without leading or trailing spaces that can contain any number of embedded spaces.