1

I would like to extract IPV4 address from each line. There could be valid or invalid ip addresses, I would like to print them all.

e.g.

Failed service from 212.345.23.234 
Successfully logged in at 13:09:89 from 12.34.76.54 
Ping from 123.567.42.56
.....
.....

Output:
212.345.23.234
12.34.76.54
123.567.42.56
.....

I need a regular expression that I can run against each line straight out like currentLine.contains(PATTERN). I don't want to tokenize the string into tokens and run RE against each token.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25

3 Answers3

1

This SO question should point you the way on how you can do match host names or IPV4 address. For IPV6, take a look at this question on SO.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
0

If IP is always at the and of the line you could do it this way

String ip = str.substring(str.lastIndexOf(' ') + 1);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

matching ip with regex...i had to roll my own. it seems to work for all ip ranges, though cumbersome. let me know if it fails for any case as i'll need to update my own:

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

this doesnt extract anything, just matches. its up to you to do what you need.

gwillie
  • 1,893
  • 1
  • 12
  • 14