Q:

Regex for tabular content using Javascript

I am trying to get the specific values from the .txt file using NodeJS. Hope using Regex is the best way to achieve this. So i tried below code from my side, here i can able to get the values from non-tabular area .
var res4 = data.match(/\d*.\d*%id/g);
var res5 = data.match(/\d*\d*k free/g);
console.log(res4); **// [ '93.2%id' ]**
console.log(res5); **//[ '862100k free', '6143996k free' ]**

But when i am trying to get the value inside tabular format of the .txt file , i am unable to do it. Below is the source .txt file for your reference. Here i need cuic_reporting(in command column) and its corresponding %CPU column value for it .Kindly help me in it. Thanks.
 top - 02:51:10 up 176 days, 23:47,  1 user,  load average: 0.13, 0.09, 0.07
Tasks: 208 total,   1 running, 207 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.7%us,  1.9%sy,  0.1%ni, 93.2%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:  16336156k total, 15474056k used,   862100k free,   362908k buffers
Swap:  6143996k total,        0k used,  6143996k free,  8415656k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
20967 OSAdmin   30  10 1299m 120m  26m S 35.7  0.8   0:03.47 java
14231 tomcat    20   0 3422m 3.1g  22m S  2.0 20.0  10869:37 cuic_reporting
    1 root      20   0 19572 1812 1244 S  0.0  0.0   2:59.65 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0  25:22.51 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   2:42.43 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0
    6 root      RT   0     0    0    0 S  0.0  0.0   0:14.34 watchdog/0
    7 root      RT   0     0    0    0 S  0.0  0.0  25:47.89 migration/1
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/1

Thanks !!..

A:

Note that your first pattern can be written as \b\d+(?:\.\d+)?%id\b using a single \d+ matching 1+ digits where you have to escape the dot to match it literally.
If the value can also be without a decimal part, you can make that optional
The second pattern could be written as \b\d+k free\b using a single \d+ to match 1+ digits as well. You could use word boundaries to prevent the word being part of a larger word and get partial matches.

To get the value of cuic_reporting you could use a capturing group and use a repeating group to match the values between the %CPU and cuic_reporting.
(\d+(?:\.\d+)?)(?:[^\S\r\n]+\S+){2}[^\S\r\n]+cuic_reporting\b

Regex demo

let str = ` top - 02:51:10 up 176 days, 23:47,  1 user,  load average: 0.13, 0.09, 0.07
Tasks: 208 total,   1 running, 207 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.7%us,  1.9%sy,  0.1%ni, 93.2%id,  0.0%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:  16336156k total, 15474056k used,   862100k free,   362908k buffers
Swap:  6143996k total,        0k used,  6143996k free,  8415656k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
20967 OSAdmin   30  10 1299m 120m  26m S 35.7  0.8   0:03.47 java
14231 tomcat    20   0 3422m 3.1g  22m S  2.0 20.0  10869:37 cuic_reporting
    1 root      20   0 19572 1812 1244 S  0.0  0.0   2:59.65 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0  25:22.51 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   2:42.43 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0
    6 root      RT   0     0    0    0 S  0.0  0.0   0:14.34 watchdog/0
    7 root      RT   0     0    0    0 S  0.0  0.0  25:47.89 migration/1
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/1`;

let pattern = /(\d+(?:\.\d+)?)(?:[^\S\r\n]+\S+){2}[^\S\r\n]+cuic_reporting\b/;
console.log(str.match(pattern)[1]);

