Home > database >  Regex to extract last IP address in a multiline string [duplicate]
Regex to extract last IP address in a multiline string [duplicate]

Time:09-28

I am new to Javascript and I am trying to extract the last IP address found in a multiline string.

Here is the syntax I am using but it's giving me the first IP address found instead:

\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b

Sample data:

ARC-Authentication-Results: i=1; mx.google.com;
       dkim=pass header.i=@accounts.google.com header.s=20210112 header.b=xAkCBCFz;
       spf=pass (google.com: domain of 3icnryqgtc_4tu-xkvr4giiu0tzy.muumrk.iussujngx.qngtmsgor.ius@gaia.bounces.google.com designates 209.85.220.73 as permitted sender) smtp.mailfrom=3IcNRYQgTC_4tu-xkvr4giiu0tzy.muumrk.iussujngx.qngtmsgor.ius@gaia.bounces.google.com;
       dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=accounts.google.com
Return-Path: <3IcNRYQgTC_4tu-xkvr4giiu0tzy.muumrk.iussujngx.qngtmsgor.ius@gaia.bounces.google.com>
Received: from mail-sor-f73.google.com (mail-sor-f73.google.com. [209.85.220.73])
        by mx.google.com with SMTPS id c22sor8709897pgl.49.2021.09.27.06.12.02
        for 
        (Google Transport Security);
        Mon, 27 Sep 2021 06:12:02 -0700 (PDT)
Received-SPF: pass (google.com: domain of 3icnryqgtc_4tu-xkvr4giiu0tzy.muumrk.iussujngx.qngtmsgor.ius@gaia.bounces.google.com designates 209.85.220.73 as permitted sender) client-ip=209.85.220.73;
Authentication-Results: mx.google.com;
       dkim=pass [email protected] header.s=20210112 header.b=xAkCBCFz;
       spf=pass (google.com: domain of 3icnryqgtc_4tu-xkvr4giiu0tzy.muumrk.iussujngx.qngtmsgor.ius@gaia.bounces.google.com designates **209.85.220.73** as permitted sender) smtp.mailfrom=3IcNRYQgTC_4tu-xkvr4giiu0tzy.muumrk.iussujngx.qngtmsgor.ius@gaia.bounces.google.com;
       dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=accounts.google.com

The ip address I am trying to get back with regex is the last one in the text above.

CodePudding user response:

If you unset de m regex option (so only g) and end your pattern with $, it should match only the last ip :

const regex = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b$/g;

CodePudding user response:

You could refactor your current pattern to only match addresses that appear exactly at the end of your string by appending the $ token:

\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b$

Seeing as you have a trailing white space at the end of your string you may or may not need to apply something like trim() on the string before extracting, or modifying the pattern to account for such a white space at the end before the end of the string.

CodePudding user response:

Try this in browser console. I guess you can use this logic.

var IP = `network address 1: 101.21.10.128
network address 2: 218.51.46.26
network address 823: 10.54.78.105 `;

IP = IP.split(': ');

IP[IP.length-1].replace(/ /gi, '');

CodePudding user response:

Can also be seen as a duplicate of extract ip-address from a string using regex

You can use the regex: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b with the flags: gm (global and multiline):

e.g.

var r = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/gm;
var IP = `network address 1: 101.21.10.128
network address 2: 218.51.46.26
network address 823: 10.54.78.105 `;
var t = IP.match(r);
console.log(t);

output would be:

['101.21.10.128', '218.51.46.26', '10.54.78.105']
  • Related