0

I'm try to selecting a large text and extract all the IP from this text. E.g

fdsfsfsdfsd 36.23.227.234 Paris,FR FKGNGH 2df2df5cdsss 12151281250 November 23d, 2014 November 23d, 2014 titlethere 6928699 dfgdfgdfg REWG50 US$50.00
fdsfddfseed 96.8.225.128 London,UK FDGSDS ASDGSDG22GDS 33583855464 January 30d, 2011 January 30d, 2011 titlethere 34576874 dsfasdg ASASDF41 US€0.00

the result would be 36.23.227.234 96.8.225.128

Is this possible ? as the data is very random ? can AppleScript or maybe more javascript I'm guessing can do this?

Kevin
  • 1,076
  • 4
  • 22
  • 49

1 Answers1

1

You can use regular expressions match() function in JavaScript:

var str = 'fdsfsfsdfsd 36.23.227.234 Paris,FR FKGNGH 2df2df5cdsss 12151281250 November 23d, 2014 November 23d, 2014 titlethere 6928699 dfgdfgdfg REWG50 US$50.00 fdsfddfseed 96.8.225.128 London,UK FDGSDS ASDGSDG22GDS 33583855464 January 30d, 2011 January 30d, 2011 titlethere 34576874 dsfasdg ASASDF41 US€0.00';
var regexp = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/gi;
var matches_array = str.match(regexp);
console.log(matches_array);

Which gives you Array [ "36.23.227.234", "96.8.225.128" ]

See https://stackoverflow.com/a/41610014 for all occurences of a string and https://stackoverflow.com/a/32689475 for regex to find IP addresses.

Community
  • 1
  • 1
Asmund
  • 158
  • 1
  • 5
  • Thank, that seems to work well with the demo line I wrote, but when I try in live with large data I have this "Error on line 1: SyntaxError: Unexpected EOF" – Kevin Feb 22 '17 at 08:48
  • Looks like an error because of the data being over multiple lines (see http://stackoverflow.com/a/26325623/1619146). I guess I massaged your test data to remove this. You could add a backslash (\) to each new line to keep everything in one string. This will take some manual work or maybe you can do it from where you output your data? – Asmund Feb 22 '17 at 09:07
  • Thank you, yes I will find a way to add a backslash some how, thank you. – Kevin Feb 22 '17 at 09:44