I'm formatting the output of an SMTP server log for output on a secured website. I already formatted the IP addresses with and without added port numbers (123.123.123.123 and 123.123.123.123:456, /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\d{1,3}\.\d{1,3}\.\d{1,3}/
).
Now I need to format other numeric values, but not in combination with non-numeric characters like ID's and CRAM-MD5
).
In the following example, I need to get the 100, 56, 0, but neither the 5 of CRAM-MD5
, nor the 21 (or 21E9) of 21E9C126E0B80
, aAnd I need the 0 after Client
and the 2022070508301657009855590.
2022-07-05 12:00:00 New Client Rejected (192.241.222.210 [digitalocean.com] -> AbuseIPDB Score: 100)
2022-07-05 12:00:00 New Client Connected (137.184.30.176 [digitalocean.com] -> AbuseIPDB Score: 56)
2022-07-05 12:00:00 New Client Connected (192.168.10.12 [] -> AbuseIPDB Score: 0)
2022-07-05 12:00:00 250-AUTH LOGIN PLAIN CRAM-MD5
2022-07-05 12:00:00 250 2.0.0 Ok: queued as 21E9C126E0B80
2022-07-05 12:00:00 Client 0 from 192.168.10.12 Disconnecting
2022-07-05 12:00:00 Forward mail 2022070508301657009855590
I currently have the following regex, which gets me 100
only: / [^a-zA-Z\/\.>(]\d+[^a-zA-Z\/\.>)\-]/
Yes, I need a space in front and exclude the >
to avoid formatting an already formatted string. And yes, there need to be some follow-up characters excluded.
Here is my code:
preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\d{1,3}\.\d{1,3}\.\d{1,3}/', $sLog, $matches);
foreach ($matches[0] as $nr) {
$sLog = str_replace($nr, '<span class="number">' . $nr . '</span>', $sLog);
}
The test scenario is here: https://regex101.com/r/sbD10s/1.
The regex will be used inside preg_match_all()
.
Can anyone help me on finding the correct regex?
My Answer:
To extract only the numeric values from the given string, you can use regular expressions in PHP. Here's an example code snippet that achieves this:
php
$string = "2022-07-05 12:00:00 New Client Rejected (192.241.222.210 [digitalocean.com] - 100 56 0 CRAM-MD5 21E9C126E0B80 Client 0 2022070508301657009855590";
preg_match_all('/\b\d+\b/', $string, $matches);
$numericValues = $matches[0];
print_r($numericValues);
In this code snippet:
- The regular expression `\b\d+\b` is used to match one or more digits that are surrounded by word boundaries.
- `preg_match_all` function is used to find all matches of the regular expression in the given string.
- The extracted numeric values are stored in the `$numericValues` array.
- Finally, the extracted numeric values are printed using `print_r`.
When you run this code with the given example string, it will output:
Array
(
[0] => 2022
[1] => 07
[2] => 05
[3] => 12
[4] => 00
[5] => 00
[6] => 192
[7] => 241
[8] => 222
[9] => 210
[10] => 100
[11] => 56
[12] => 0
[13] => 0
[14] => 2022070508301657009855590
)
You can further process or manipulate these extracted numeric values as needed for your application.
Rate this post
4 of 5 based on 7980 votesComments