Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:35+00:00 2026-06-13T09:03:35+00:00

today i’m trying to parse a ‘ps aux’ output (issued from a node.js server)

  • 0

today i’m trying to parse a ‘ps aux’ output (issued from a node.js server)

so i made an happy regex to parse the values, and i wanted to put all those fancy informations into a simple JSON object 🙂

problem is, the regex i’ve made has 3 match groups, doing multiple matches… and i don’t even know if it’s possible to have it all the matches returned.

var spawn = require("child_process").spawn;

function exec(http_req, http_resp){
    var re_ps = /(?:([\d\.\w\-\?\/\+]+)\s+){8}(?:((?:\d+\:\d+)|(?:\w+\d+))\s+){2}([^\n]+)/g,
        result_re_ps,
        array_re_ps = [];
    //[USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT], dates[START, TIME], COMMAND
    var ps_process = spawn("ps", ["aux"]);
    ps_process.stdout.on("data", function(data){
        var split_data = data.toString().split('\n');
        for (var i=1; i < 2; i++){
            while ((result_re_ps = re_ps.exec(split_data[i])) != null){
                console.log("--- while iteration ---");
                console.dir(result_re_ps);
                //console.dir(result_re_ps);
            }
        }
        //http_resp.write(data);
    });
    ps_process.on("exit", function (code) {
        if (code !== 0) {
            console.log("ps_process exited with code " + code);
        }
        http_resp.end();
    });
}

exports.exec = exec;

the “— while iteration —” it’s just one per each line, instead, it should be multiples!

I will link a fancy screenshot to let you visually understand what kind of data i’m expecting enter image description here

As you can see the above are 3 groups of data, each one with multiple occurrences

Any help? :p

EDITED TO CLARIFY:

This is the result of a line like:

root      5213  0.0  2.1   8688  2760 ?        Ss   11:33   0:01 sshd: root@pts/0 

enter image description here

As you can see, the resulting array is 1-dimensional, instead for the results to be contained entirely it HAS to be at least 2-dimensional

UPDATE:

For now i’ve solved the problem changin my code like this:

var spawn = require("child_process").spawn;

function exec(http_req, http_resp){
    var array_re_ps = [];
    var re_ps1 = /(?:([\d\.\w\-\?\/\+]+)\s+)/g,
        re_ps2 = /(?:((?:\d+\:\d+)|(?:\w+\d+))\s+)/g,
        re_ps3 = /([^\n]+)/g;
    //[USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT], dates[START, TIME], COMMAND
    var ps_process = spawn("ps", ["aux"]);
    ps_process.stdout.on("data", function(data){
        var split_data = data.toString().split('\n');
        for (var i=20; i < 21; i++){
            array_re_ps[0] = [], array_re_ps[1] = [];
            for (var j=0; j<8; j++){
                array_re_ps[0].push(re_ps1.exec(split_data[i])[1]);
            }
            re_ps2.lastIndex = re_ps1.lastIndex;
            for (var j=0; j<2; j++){
                array_re_ps[1].push(re_ps2.exec(split_data[i])[1])
            }
            re_ps3.lastIndex = re_ps2.lastIndex;
            array_re_ps[2] = re_ps3.exec(split_data[i])[1];
            console.dir(array_re_ps);
        }
    });
    ps_process.on("exit", function (code) {
        if (code !== 0) {
            console.log("ps_process exited with code " + code);
        }
        http_resp.end();
    });
}

exports.exec = exec;

Basically i split my long regexp, in 3 smaller ones, and i do cycle through their results.
If you try the code i formatted the resulting array exactly as i wanted, but i still think that it’s a side-solution.

Since Regular Expressions lets you “multiple parse group multiple times”, it is possible to have that functionality in javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:03:40+00:00Added an answer on June 13, 2026 at 9:03 am

    Take a look at the return values of the exec function.

    It returns the index where it first matched, the original string and the capture groups in the elements [0]...[n].

    All this is available in your output, you just have to iterate through the single elements of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I am trying to remove some bytes from an EXE file. Inside the
Today I made a code that should do a login but sadly it wont.
Today I made some tests and I am curious of the results. I made
Today I added a class which manages the connection to a MySQL Server. It
Today I migrated an old application from EF 4.2 to EF 4.3.1. In my
Today, I saw this article on Twitter about getting Webm data from a canvas:
Today while investigating how/where to download and save audio/image files from a URL in
Today I discovered that my fresh installation of Apache HTTP Server is able to
Today is friday and I'm a desperate sysadmin. I issued a demand of certificate
today I was trying to use the SnowballAnalyzer on Lucene Java API v3.6.0 but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.