xxxxxxxxxx
import java.util.Scanner;
class MyRegex {
// Define the regular expression pattern for a valid IP address
String pattern = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
}
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String IP = in.next();
System.out.println(IP.matches(new MyRegex().pattern));
}
}
}
xxxxxxxxxx
function valid_ip(ipString) {
if (!ipString || typeof ipString !== 'string') {
return false;
}
const splitValues = ipString.split('.');
if (splitValues.length !== 4) {
return false;
}
for (let i = 0; i < splitValues.length; i++) {
const tempForParsing = parseInt(splitValues[i], 10);
if (isNaN(tempForParsing) || tempForParsing < 0 || tempForParsing > 255) {
return false;
}
}
return true;
}
// Test IP here
const result = valid_ip("192.0.2.146");
console.log(result);
xxxxxxxxxx
try:
if len(IP.split('.')) == 4:
print(IP)
if all([bool(1) if(str(int(s)) == s and 0 <= int(s) <= 255) else bool(0) for s in IP.split('.')]):
return "IPv4"
if len(IP.split(':')) == 8:
if all([bool(1) if(len(s) <= 4 and int(s, 16) >= 0) else bool(0) for s in IP.split(':')]):
return "IPv6"
except:
return "Neither"
return "Neither"