Wednesday, April 21, 2021

Extract IP address from text with regular expression and TextPad

I have a text file like the below, we want to extract the IP's from this :


[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:19:16

[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:19:03

[Service blocked: CP] from source 192.168.1.16, Wednesday, Apr 21,2021 04:18:52

[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:18:40

[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:18:12

[Service blocked: CP] from source 192.168.1.16, Wednesday, Apr 21,2021 04:17:47

[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:17:39

[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:17:00

[Service blocked: CP] from source 192.168.1.16, Wednesday, Apr 21,2021 04:16:42

[Service blocked: CP] from source 192.168.1.51, Wednesday, Apr 21,2021 04:16:38

[LAN access from remote] from 179.60.150.46:44350 to 192.168.1.15:5001, Wednesday, Apr 21,2021 04:16:27


To do this :


  • Ctrl+H
  • Find what: ^.+?((?:\d+\.){3}\d+).+$
  • Replace with: $1
  • check Wrap around
  • check Regular expression
  • DO NOT CHECK . matches newline
  • Replace all

 

View Example Here


Explanation:

^           : beginning of line
.+?         : 1 or more any character but newline
(           : start group 1
  (?:       : start non capture group
    \d+     : 1 or more digit
    \.      : a dot
  ){3}      : end group, must appear 3 times
  \d+       : 1 or more digit
)           : end group 1
.+          : 1 or more any character but newline
$           : end of line

Replacement:

$1          : content of group 1 (ie. the IP)

Result for given example:

54.246.81.158
175.36.129.24

No comments:

Post a Comment