使用正则表达式验证和确认 IP 地址

以下正则表达式检查 IPv4.

private static final Pattern PATTERN = Pattern.compile(
^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$);

public static boolean validate(final String ip) {
return PATTERN.matcher(ip).matches();
}

Description:

^ #Début< br /> ( # 组开始 #1
[01]?\\d\\d? # 可以是一位或两位数字。如果数字大于 100,则应以 0 或 1
# 开头,例如 ([0-9], [0-9][0-9],[0-1][0-9][0-9])
|# ...or
2[0-4]\\d # 以 2 开头,后跟 0-4,以任意数字结尾
|# ...or
25[0-5] # 以 2 开头,后跟 5 并以 0-5 结尾< /> ) # 组 #2 \ 的结尾。# 后跟句点 .”
.... # 重复 3 次 (3x)
#fin


Commentaires (12)

Connectez-vous pour commenter

Rejoignez la discussion et partagez vos connaissances avec la communauté

JD
Jean Dupont Il y a 2 heures

Excellent tutoriel !

👍 12 Répondre Signaler
CodeurJava ✓ Auteur • Il y a 1 heure

N'hésitez pas si vous avez des questions.