How can I write a regex to extract data preceding and following a specific expression in a text file?

Marco Moldenhauer

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.*Temperatures\s*(.*)/gm;
const matches = Array.from([...logFileString.matchAll(regex)], x => [x[1], x[2]]);

console.log(matches);
Wiktor Stribiżew

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.*Temperatures\s*(.*)/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.*Temperatures - Astrance, 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I want to write the regex expression for the following text

Python - How to write a regex expression to extract the words following last comma?

How can I write regex for the following statements?

How I can extract specific target number from text file

How can I use regex to extract the text following two conditions into a variable?

How can I extract data from text file?

How can I write the following lambda expression in one line?

How do I write a regex that match following text?

How to extract specific data from a text file and write into CSV using python

How can I get specific data element in a text file

How can I both extract a specific line in a text file as well as multiple lines containing a specific string?

Can I use Powershell to automatically extract an unknown string with a specific pattern from a XML file and write that string in a text file?

How can I write following vector to csv file in r

how can I write a matrix in a text file?

How can I extract specific strings from a text file and add to List?

How can I extract number from text with regular expression

How can I turn the following text file into a nice RMarkDown table?

How to extract specific data from a text file in Python?

How can I extract text between parentheses containing a specific word?

How can I look into a cell with VBA to find specific text, then extract it?

How can I extract a specific text in an html code with Selenium and Python

How can I split text into lines based on a regex expression?

How can I extract/change lines in a text file whose data are separated into fields?

How to extract key phrases following specific characters using regex in R?

How can I extract a specific part of a JSON file?

How can I use a batch file to write to a text file?

How can I write the text file content equal to file name?

How to write generic regex to extract the data in ExtractText?

Extract specific data from a text file