Home > Software engineering >  python regex pattern to get stracer output
python regex pattern to get stracer output

Time:10-20

write(1, "\33(B\33[0;1m\33[90m3.8\33[4;52H", 24) = 24

I would like to get each value in between () the string before () and the number after the =

Sometimes it can be a -#

Sample of out

close(4)                                = 0

openat(AT_FDCWD, "/proc/9392/statm", O_RDONLY) = 4

read(4, "2387 878 750 226 0 163 0\n", 512) = 25

read(4, "", 487)                        = 0

close(4)                                = 0

openat(AT_FDCWD, "/proc/9392/stat", O_RDONLY) = 4

read(4, "9392 (strace) S 9330 9330 9 3481"..., 2048) = 319

read(4, "", 1729)                       = 0

close(4)                                = 0

openat(AT_FDCWD, "/proc/9407/task", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 4

What I have tried:

for the args

"(.*?)"

string at the beginning

(.*?)\(

I just need the number.

Are there more efficient ways of what I'm doing?

CodePudding user response:

Not totally sure what exactly you're looking for but this seems to work:

import re


pattern = re.compile(r"(.*)\((.*)\)\s*=\s*(.*)")


lines = ['close(4)                                = 0',
         'openat(AT_FDCWD, "/proc/9392/statm", O_RDONLY) = 4',
         'read(4, "2387 878 750 226 0 163 0\\n", 512) = 25',
         'read(4, "", 487)                        = 0',
         'close(4)                                = 0',
         'openat(AT_FDCWD, "/proc/9392/stat", O_RDONLY) = 4',
         'read(4, "9392 (strace) S 9330 9330 9 3481"..., 2048) = 319',
         'read(4, "", 1729)                       = 0',
         'close(4)                                = 0',
         'openat(AT_FDCWD, "/proc/9407/task", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 4']


for line in lines:
    print(pattern.findall(line)

Output:

[('close', '4', '0')]
[('openat', 'AT_FDCWD, "/proc/9392/statm", O_RDONLY', '4')]
[('read', '4, "2387 878 750 226 0 163 0\\n", 512', '25')]
[('read', '4, "", 487', '0')]
[('close', '4', '0')]
[('openat', 'AT_FDCWD, "/proc/9392/stat", O_RDONLY', '4')]
[('read(4, "9392 ', 'strace) S 9330 9330 9 3481"..., 2048', '319')]
[('read', '4, "", 1729', '0')]
[('close', '4', '0')]
[('openat', 'AT_FDCWD, "/proc/9407/task", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY', '4')]
  • Related