is there a smart way (maybe with java8 streams) to extract the first ipAddress in a string with a format similar to that one:
some string, 151.0.247.187, 54.239.167.92, 52.49.172.244
Thanks
is there a smart way (maybe with java8 streams) to extract the first ipAddress in a string with a format similar to that one:
some string, 151.0.247.187, 54.239.167.92, 52.49.172.244
Thanks
I discourage using split
because then you always will depend upon the index value so to me ,a better option is regex
String data="some string, 151.0.247.187, 54.239.167.92, 52.49.172.244";
Pattern pattern=Pattern.compile("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})");
Matcher matcher = pattern.matcher(data);
if (matcher.find()) {
System.out.println(matcher.group());
}
output :
151.0.247.187
1.) look for the Ip pattern
2.) display the first group
Note : there are many IP patterns available online , you can pick any suitable one