Search code examples
regexregex-lookaroundsregex-group

regex: How to find all matched groups (values: time and value for specific service)


I want to select time and value for specific row by this regular expression time: Tue 22 Jun 11:05:01 EDT 2021 usage memory for k8s_POD_eventing-kafka-cp-kafka example 30KiB

((?<time>(.*?)EDT 2021)((.|\n)*)(?<memoryblock>(?<=(eventing-kafka-cp-kafka)).*\%(\s)*(?<memory>.*)\s(?=\/).*(?=15\.51GiB)))

Text:

=======================
Tue 22 Jun 11:05:01 EDT 2021


====================
=== DOCKER STATS ===
====================
CONTAINER ID        NAME                                                                                                                                       CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
0c3f02131388        k8s_istio-proxy_istio-ingressgateway-759f7bff9f-l894p_istio-system_81979eee-1d7c-4eae-af8d-18d67dee1838_0                                  0.27%               80.34MiB / 200MiB     40.17%              0B / 0B             0B / 0B             47
4d06eb9f902f        k8s_POD_istio-ingressgateway-759f7bff9f-l894p_istio-system_81979eee-1d7c-4eae-af8d-18d67dee1838_0                                          0.00%               40KiB / 15.51GiB      -system_a9af3319-421a-492b-a8d2-5ed450509c31_0                                             0.37%               42.81MiB / 100MiB     42.81%              0B / 0B             0B / 0B             25
139321b972fa        k8s_POD_istio-pilot-854fd575d9-tjgb5_istio-system_a9af3319-421a-492b-a8d2-5ed450509c31_0                                                   0.00%               44KiB / 15.51GiB      0.00%               0B / 0B             0B / 0B             1
8045a70d159c        k8s_istio-proxy_istio-policy-54b6cdfd7c-hjbp2_istio-system_590ce59c-0fc9-462a-976c-7bf79dd20d6d_0                                          0.22%               25.97MiB / 200MiB     12.99%              0B / 0B             0B / 0B             42
60be0e4ab808        k8s_mixer_istio-policy-54b6cdfd7c-hjbp2_istio-system_590ce59c-0fc9-462a-976c-7bf79dd20d6d_0                                                0.02%               24.12MiB / 100MiB     24.12%              0B / 0B             0B / 0B             16
f0f5c7bdf247        k8s_POD_istio-policy-54b6cdfd7c-hjbp2_istio-system_590ce59c-0fc9-462a-976c-7bf79dd20d6d_0                                                  0.00%               44KiB / 15.51GiB  
d72a5583d634        k8s_POD_eventing-kafka-cp-kafka-2_default_7c6d5663-02a0-4013-923c-2b56d1e54ed4_0                                                           0.00%               30KiB / 15.51GiB     
====================
=== MEMORY USAGE ===
====================
top - 11:10:04 up 96 days, 18:31,  0 users,  load average: 0.76, 0.78, 0.77

=======================
Tue 22 Jun 11:10:01 EDT 2021


====================
=== DOCKER STATS ===
====================
CONTAINER ID        NAME                                                                                                                                       CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS

0c3f02131388        k8s_istio-proxy_istio-ingressgateway-759f7bff9f-l894p_istio-system_81979eee-1d7c-4eae-af8d-18d67dee1838_0                                  0.22%               80.34MiB / 200MiB     40.17%              0B / 0B             0B / 0B             47
4d06eb9f902f        k8s_POD_istio-ingressgateway-759f7bff9f-l894p_istio-system_81979eee-1d7c-4eae-af8d-18d67dee1838_0                                          0.00%               40KiB / 15.51GiB      0.00%               0B / 0B             0B / 0B             1
b92036f42af2        k8s_cp-kafka-broker_eventing-kafka-cp-kafka-2_default_7c6d5663-02a0-4013-923c-2b56d1e54ed4_0                                               14.67%              643.3MiB / 10GiB      6.28%               0B / 0B             0B / 0B             76
d72a5583d634        k8s_POD_eventing-kafka-cp-kafka-2_default_7c6d5663-02a0-4013-923c-2b56d1e54ed4_0                                                           0.00%               40KiB / 15.51GiB     

However, It selects first time value and last memory value

Expected:

time: Tue 22 Jun 11:05:01 EDT 2021

memory: 30KiB

time: Tue 22 Jun 11:10:01 EDT 2021

memory: 40KiB

Actual:

time: Tue 22 Jun 11:05:01 EDT 2021

memory: 40KiB


Solution

  • You can match as least as possible lines from the start date with EDT until you match a line with eventing-kafka-cp-kafka and that has the value 15\.51GiB after matching the memory part.

    ^(?<time>.* EDT 2021)(?:\n.*)*?\n.*eventing-kafka-cp-kafka.* (?<memory>\d+[^\n/]*) / 15\.51GiB
    
    • ^ Start of string
    • (?<time>.* EDT 2021) Named group time
    • (?:\n.*)*? Match as least as possible lines
    • \n.*eventing-kafka-cp-kafka Match a newline with the literal text
    • .* (?<memory>\d+ [^\n/]*) Named group memory, match 1+ digits and till the first /
    • / 15\.51GiB Match literally

    Regex demo