skip to Main Content

Problem:
I have a logfile containing data and I want to extract the data preceding and following a specific expression. I’ve taken a screenshot of the logfile and highlighted the data I want. You can view the screenshot of the logfile here: logfile.JPG. Since the length, format, and position of the data never change in this line, I am simplifying by showing a specific line from the logfile:

Aug 23 07:25:43 Astrance 125_Heater_1.30.02.0124_d[11421]: 2023-08-23T07:25:43.402421|304|4|1250025|virtual bool CReadTempStep::SPIIsSatisfied():129|Temperatures 39.998318 39.982665 40.167597 40.167027

I am interested in extracting the timestamp at the beginning Aug 23 07:25:43 and the four numbers at the end 39.998318 39.982665 40.167597 40.167027. I’m looking for a single regular expression that can return one match.

My solution so far written in JavaScript:

Regex1, Getting the timestamp (Aug 23 07:25:43):

.*(?= Astrance.*Temperatures)

Regex2, Getting the four numbers (39.998318 39.982665 40.167597 40.167027):

(?<= Astrance.*Temperatures).*

Question:

How can I combine Regex1 and Regex2 into a single regex that returns one match?

Expected answer:

It would be great if somebody provides me a tiny javascript program which returns me all the matches in a list as follows:

MatchList = [
   [ 'Aug 23 07:25:43', '39.998318 39.982665 40.167597 40.167027' ],
   [ 'Aug 23 07:25:45', '39.999999 39.576576 40.676767 40.334444' ],
   [ 'Aug 23 07:25:47', '39.456777 39.734534 40.898899 40.898999' ],
    ...
   ]

Solution (Thanks to the community):

// read local logfile into a variable:

var fs = require("fs");
var logFileString = fs.readFileSync("./sys", "utf-8");

// use regex and write the matches into a list:

const regex = /^(.*?)s*Astrance.*Temperaturess*(.*)/gm;
const matches = Array.from([...logFileString.matchAll(regex)], x => [x[1], x[2]]);

console.log(matches);

2

Answers


  1. Hi here is a working example:

    const string = 'Aug 23 07:25:43 Astrance 125_Heater_1.30.02.0124_d[11421]: 2023-08-23T07:25:43.402421|304|4|1250025|virtual bool CReadTempStep::SPIIsSatisfied():129|Temperatures 39.998318 39.982665 40.167597 40.167027'
    
    const pattern = /(.*(?= Astrance))|((?<=Temperatures ).*)/g
    
    
    console.log(string.match(pattern)[0])
    console.log(string.match(pattern)[2])
    Login or Signup to reply.
  2. You can use

    const text = 'Aug 23 07:25:43 Astrance 125_Heater_1.30.02.0124_d[11421]: 2023-08-23T07:25:43.402421|304|4|1250025|virtual bool CReadTempStep::SPIIsSatisfied():129|Temperatures 39.998318 39.982665 40.167597 40.167027';
    const regex = /^(.*?)s*Astrance.*Temperaturess*(.*)/gm;
    const matches = Array.from([...text.matchAll(regex)], x => [x[1], x[2]]);
    console.log(matches);

    See the regex demo. Details:

    • ^ – start of any line (since the m flag is used)
    • (.*?) – Group 1: any zero or more chars other than line break chars as few as possible
    • s* – zero or more whitespaces
    • Astrance.*TemperaturesAstrance, then any zero or more chars other than line break chars as many as possible and then Temperatures
    • s* – zero or more whitespaces
    • (.*) – Group 2: any zero or more chars other than line break chars as many as possible.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search