简体   繁体   中英

Regex Pattern Needed

hope someone is able to help out finding this regex pattern: If i have this requests:

1-15 20241 0/0/6205 W 1.13 1071 0 0.00 11138.15 118.100.162.102 domain.com:443 GET /file/192493511503562/ HTTP/1.1

2-15 6252 0/130/6078 W 5.50 387 0 0.0 490.28 16798.43 14.33.181.155 domain.com:443 GET /file/720658522052690/ HTTP/1.1

3-14 9649 0/157/5992 G 4.37 1807 0 0.0  296.81 13625.69 212.252.56.41 domain.com:443 GET /file/512530584447085/ HTTP/1.1

another sample output (by source code)

0-16 1005 0/59/6014 W 2.15 648 0 0.0 114.91 10535.48 178.235.43.134   domain.com:443 GET /file/152577842120081/ HTTP/1.1   1-16 8820 0/22/6489 W 0.96 1790 0 0.0 100.79 11996.02 177.221.26.229   domain.com:443 GET /file/415757652884349/ HTTP/1.1   2-16 28288 0/35/6723 W 1.46 178 0 0.0 68.29 18010.46 89.3.212.185   domain.com:443 GET /file/687013016222044/ HTTP/1.1   3-16 10274 0/0/6736 W 0.07 2275 0 0.0 0.00 14280.77 46.176.105.15   domain.com:443 GET /file/321646937401965/ HTTP/1.1   4-16 29945 0/2/7471 W 0.02 210 0 0.0 0.01 16350.10 190.158.29.250   domain.com:443 GET /file/333674884214997/ HTTP/1.1   5-16 29245 0/2/7699 W 0.01 272 0 0.0 1.07 20284.17 49.48.250.12   domain.com:443 GET /file/781792728276923/ HTTP/1.1

There is just one space between. I need to get those values:

20241   
192493511503562

6252  
720658522052690

9649
512530584447085 

So currently my preg_match_all looks like:

preg_match_all('@[0-9] (.*?) [0-9]/@si', $output, $url) ;  
foreach($url[1] as $output )  {  echo $output ; } 

How can i get both values? The current preg_match only echos the first value. So for example 20241 6252 .. etc. I tried this

preg_match_all('@[0-9] (.*?) [0-9]*/(.*?)/@si', $output, $url) ; 
foreach($url[0] as $output )  {   echo $output ; } 

but still not the result i want with [0]. Maybe some of your genuise people can help out.

EDIT: To clarify this is the output from exec('/usr/local/apache/bin/apachectl fullstatus', $output); foreach ($output as $output ) { and the code } .. So i just need the PID and REQUEST of an apache process

I'll avoid the "at least try..." spiel. But here's a start for you:

/^\d{1,}-\d{2,} (\d+) .+ \/file\/(\d+)\/ .+$/

Basically we're looking for your starting ID (im guessing its an ID) such as 1-15 then capturing the number after. Then looking for /file/.../ and capturing the number there too.

You will need to alter this for all you data as we only have 3 lines to work on. Nor is it perfect. But hopefully enough to get you on your way...

https://regex101.com/r/dE8lE2/1

Here is a pattern that I like because it is reusable in different contexts: ^(?:\\S+\\s+){1}(\\S+\\s+){1}(?:\\S+\\s+){11}. ?(\\d+). $

This says:

  • ^ - 'From the beginning of the line'
  • (?:\\S+\\s){1} - Consume one column without capturing - \\S+ says 'get one or more non-space characters' and \\s+ 'get one or more space characters'the parens and {1} are not strictly necessary but facilitate re-use in the event that you wanted to skip the first 'n' columns
  • (\\S+\\s) - Capture the value in the next column (column 2)
  • (?:\\S+\\s){11} - Skip the next 11 columns
  • .*?(\\d+) - consume the minimum number of any character up to but not including a number; then capture one or more successive numbers
  • .*$ - identify the remainder of characters until the end of the line ($) - this is not strictly necessary but useful if wanting to use a find/replace tool to see result of line transformation

The captured parts of the pattern are then the 2nd column and the numeric part of the 14th column (there are multiple ways to capture the numeric part as noted in at least one other answer).

Hope this helps.

[\d-]+\s+(\d+).+?(?:GET|POST|HEAD|PUT).+?/([^/]+)/?\s+HTTP[^\s]+

Regex live demo

PHP live demo

Here is a possible way to do it:

preg_match_all('/\S+\s+(\d+).*?file\/(\d+).*?$/m', $output, $url) ;

foreach(array_combine($url[1], $url[2]) as $num1 => $num2) {
    echo "$num1 $num2\n";
};

Output for the sample data:

20241 192493511503562
6252 720658522052690
9649 512530584447085

See it run on eval.in

This might help you.

/\d+\-\d+\s+(\d+).+\/file\/(\d+)/g

test link : https://regex101.com/r/kM3qR3/1

php:

preg_match_all('/\d+\-\d+\s+(\d+).+\/file\/(\d+)/g', $output, $url) ; 
foreach($url[1] as $k => $output )
{
    echo $output . ',' . $url[2][$k];
} 

edit:

phpfiddle: http://sandbox.onlinephpfunctions.com/code/0be40cc84a0f02b89a9181aef63c3706e607e7ab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM