import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\d{1,3}(?:\\.\\d{1,3}){2}\\.(\\d{1,3}))(?:(?:-|\\s+to\\s+)(\\d{1,3}(?![\\d\\.]))|(?:-|\\s*to\\s+)(\\d{1,3}(?:\\.\\d{1,3}){3})|\\s+(25\\d(?:\\.\\d{1,3}){3})|\\s*\\/(\\d{1,3}))?";
final String string = "ip: \"47.1.2.3\"\n"
+ "range: \"47.1.2.3-4\"\n"
+ "ip range: \"47.1.2.3-47.1.2.4\"\n"
+ "ip to range: \"47.1.2.3 to 4\"\n"
+ "ip to ip range: \"47.1.2.3 to 47.1.2.4\"\n"
+ "ip CIDR: \"47.1.2.4/32\"\n"
+ "ip Mask: \"47.1.2.4 255.255.255.255\"\n\n"
+ "what you want:\"47.1.2.3 to 4, 47.1.2.7, 47.1.3.9-47.1.3.19\"";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html