Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
RegEx extract a substring from log file

I'm working with JavaScript and I need to parse a log file in order to extract a substring.

This is an example fo my log file:

Feb 04 11:20:48 info: VPN disconnected: admin @ 6051:abcdefgh [NB017] (duration: 00:05:32)
Feb 04 12:59:21 info: VPN connected: admin @ 6051:abcdefgh [NB017]
Feb 04 12:59:21 info: Connected to 123.123.123.123 (TLS)
Feb 04 12:59:21 info: Data tunnel connected: 
Feb 04 12:59:21 info: Data tunnel operational!
Feb 04 12:59:53 info: VPN disconnected: admin @ 6051:abcdefgh [NB017] (duration: 00:00:32)
Feb 04 13:01:03 info: VPN connected: admin @ 6051:abcdefgh [NB017]
Feb 04 13:01:03 info: Connected to 123.123.123.123 (TLS)
Feb 04 13:01:03 info: Data tunnel connected: 
Feb 04 13:01:03 info: Data tunnel operational!
Feb 04 13:01:35 info: VPN disconnected: admin @ 6051:abcdefgh [NB017] (duration: 00:00:40)
Feb 04 13:49:26 info: VPN connected: admin @ 6051:abcdefgh [NB017]
Feb 04 13:49:27 info: VPN disconnected: admin @ 6051:abcdefgh [NB017] (duration: 00:00:09)
Feb 04 13:50:06 info: VPN connected: admin @ 6051:abcdefgh [NB017]
Feb 04 13:50:06 info: Connected to 123.123.123.123 (TLS)
Feb 04 13:50:06 info: Data tunnel connected: 
Feb 04 13:50:07 info: Data tunnel operational!
Feb 04 15:37:57 info: (Log Displayed)

I need to extract the user who make the last connection since there are multiple connection in the log.

For instance starting from "VPN connected: " to the last "]".

I tried with this RegEx

VPN connected([^.]+)]

but JS returns several log when I need only the last one.

How can I improve mi RegEx?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could solve this problem by taking the last capturing group from the resulting JavaScript array or by modifying your RegEx:

(VPN connected[^\]]+\])(?![\s\S]*\1)

First of all, I think you want to capture each line of VPN connected separately:

VPN connected[^\]]+\]

This will start with "VPN connected" and stops with the next closing bracket.

Then I added a negative lookahead group (indicated by ?!). The group looks for any whitespace and non-whitespace followed by our first capturing group. The negative lookahead will ensure, that we don't have another match after our first match. This results in our first match being the last occurence :-)

Edit: Clarified explanation for the first capturing group

Edit2: Since you want to have the user, you can expand the RegEx to something like this:

(VPN connected: (\w+)[^\]]+\])(?![\s\S]*\1)

The user should be found in the second group.

Note: There might be some tweaking necessary based on the allowed characters in the user's name.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!