2

I was given a regex to search through logs with. However, it works in their search but not in PHP. I'm bad with regex, and have no clue what’s wrong with it.

Their regex "\\] ([a-zA-Z _]+) \\(.*to ([a-zA-Z _]+) \\(.*\\$([0-9,]+)"

Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash

I need assistance in getting this to work in PHP.

An example of the logs is:

[2013/12/28 - 15:16:22] Zack Willson (IP: 127.0.0.1) has sold their house (ID 4681) to William Hill (IP: 127.0.0.1) for $13,700,000.

I need to obtain the first players name, the second players name, and the amount it cost. If that regex isn't easily converted or whatever the issue may be.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Rsmiley
  • 29
  • 6
  • In what system their regex is used? – chanchal118 Dec 29 '13 at 04:54
  • The solution to your immediate problem is that preg_match expects a regex to be surrounded by delimiters, which as it says, can't be a backslash. Forward slash is pretty typical, such as: `"/\\] ([a-zA-Z _]+) \\(.*to ([a-zA-Z _]+) \\(.*\\$([0-9,]+)/"` – femtoRgon Dec 29 '13 at 04:56
  • I honestly have no clue, all I know is it worked properly in [link](http://puu.sh/620lV/64bfe7f941.png) screenshot – Rsmiley Dec 29 '13 at 04:56
  • 1
    Remember you can most always google error messages: [Delimiter must not be alphanumeric or backslash](https://www.google.com/search?q=site:stackoverflow.com+Delimiter%20must%20not%20be%20alphanumeric%20or%20backslash) – mario Dec 29 '13 at 04:59
  • @femtoRgon - Thank you, but I tried that and it only gets the full string. However the errors stop with it. – Rsmiley Dec 29 '13 at 05:03

1 Answers1

1

How about this regex:

\]\s+([a-zA-Z\s_]+)\s+\(.*\)\s+to\s+([a-zA-Z\s_]+)\s+.*\)\s+for\s+(\$[0-9,\s_]+)\s?+

In PHP it would be:

// Test data from the log.
$log_data = "[2013/12/28 - 15:16:22] Zack Willson (IP: 127.0.0.1) has sold their house (ID 4681) to William Hill (IP: 127.0.0.1) for $13,700,000.";

// The 'preg_match' statement.
preg_match('/\]\s+([a-zA-Z\s_]+)\s+\(.*\)\s+to\s+([a-zA-Z\s_]+)\s+.*\)\s+for\s+(\$[0-9,\s_]+)\s?+/is', $log_data, $matches);

// Output the '$matches'
echo '<pre>';
print_r($matches);
echo '</pre>';

And the results would be:

Array
(
    [0] => ] Zack Willson (IP: 127.0.0.1) has sold their house (ID 4681) to William Hill (IP: 127.0.0.1) for $13,700,000
    [1] => Zack Willson
    [2] => William Hill
    [3] => $13,700,000
)
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    This is exactly what I ended up using. I tried to mark my question answered but I couldn't yet. Thank you – Rsmiley Dec 29 '13 at 05:58
  • Thanks! I just edited it a small bit to make the logic consistent. So use this version instead of whatever you grabbed before. – Giacomo1968 Dec 29 '13 at 06:20