用java语言 截取字符串中的Ip地址,并判断是否合法,请问怎么做? 例“ip addess 192.160.2.3” 判断合法
4个回答
展开全部
2步做,先用正则判断格式,比如"ip address (\\d{1,3}\\.){3}\\d{1,3}" (Java正则)
得到ip数值后,再用if 判断各位数字是否在0-255之间
if(d>=0 && d<=255) print 合法;
else print 不合法;
得到ip数值后,再用if 判断各位数字是否在0-255之间
if(d>=0 && d<=255) print 合法;
else print 不合法;
追问
字符串是任意字符串,而且我期望能得到代码,你这说的基本等于没说
追答
因为在多次回答过一样的题也给出过代码,所以没直接重写
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
static public void main(String args[]){
String t="百度知道 > 电脑/网络 > 程序设计 > JAVA相关\n用java语言 截取字符串中的Ip地址,并判断是否合法,请问怎么做?ip address 192.160.2.256 例“ip address 192.160.2.3” 判断合法检举 | 2012-5-26 15:23 提问者: guxinjushi | 悬赏分:20 | 浏览次数:47次\n回答 共3条\n2012-5-28 14:58 ip address 192.160.2.4imkow | 十三级ip address 192.160.2.5";
Matcher m=Pattern.compile("ip address ((\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}))").matcher(t);
while(m.find()){
System.out.print("找到:"+m.group(1));
boolean valid=true;
for(int i=2;i=255){
valid=false; break;
}
}
System.out.println(" =>"+(valid?"合法":"不合"));
}
}
}
==
找到:192.160.2.256 =>不合
找到:192.160.2.3 =>合法
找到:192.160.2.4 =>合法
找到:192.160.2.5 =>合法
====
你的例子address 错拼写成'addess',例子按正确address严格搜索的。
展开全部
import javax.swing.JOptionPane;
public class T {
public static void main(String as[]) {
String id = JOptionPane.showInputDialog(null, "Please input IP: ");
int add = id.length();
String test = new String();
boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
boolean b4 = false;
int found = 1;
if (id.charAt(0) == '.' || id.charAt(add - 1) == '.' || add > 15
|| id.split(".").length != 4) {
JOptionPane.showMessageDialog(null, "IPAddress is error ");
} else {
for (int i = 0; i < add; i++) {
if (String.valueOf(id.charAt(i)).equals(".")) {
// JOptionPane.showMessageDialog(null,String.valueOf(i));
int testadd = Integer.parseInt(test);
if (found == 1) {
if (testadd > 0 && testadd < 255) {
b1 = true;
}
}
if (found == 2) {
if (testadd >= 0 && testadd < 255) {
b2 = true;
}
}
if (found == 3) {
if (testadd >= 0 && testadd < 255) {
b3 = true;
}
}
test = new String();
found++;
} else {
test = test + String.valueOf(id.charAt(i));
if (i == (add - 1)) {
int testadd = Integer.parseInt(test);
if (testadd >= 0 && testadd < 255) {
b4 = true;
}
}
}
}
if (b1 && b2 && b3 && b4) {
JOptionPane.showMessageDialog(null, "IPAddress is exactly! ");
} else {
JOptionPane.showMessageDialog(null, "IPAddress is error ");
}
}
}
}
public class T {
public static void main(String as[]) {
String id = JOptionPane.showInputDialog(null, "Please input IP: ");
int add = id.length();
String test = new String();
boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
boolean b4 = false;
int found = 1;
if (id.charAt(0) == '.' || id.charAt(add - 1) == '.' || add > 15
|| id.split(".").length != 4) {
JOptionPane.showMessageDialog(null, "IPAddress is error ");
} else {
for (int i = 0; i < add; i++) {
if (String.valueOf(id.charAt(i)).equals(".")) {
// JOptionPane.showMessageDialog(null,String.valueOf(i));
int testadd = Integer.parseInt(test);
if (found == 1) {
if (testadd > 0 && testadd < 255) {
b1 = true;
}
}
if (found == 2) {
if (testadd >= 0 && testadd < 255) {
b2 = true;
}
}
if (found == 3) {
if (testadd >= 0 && testadd < 255) {
b3 = true;
}
}
test = new String();
found++;
} else {
test = test + String.valueOf(id.charAt(i));
if (i == (add - 1)) {
int testadd = Integer.parseInt(test);
if (testadd >= 0 && testadd < 255) {
b4 = true;
}
}
}
}
if (b1 && b2 && b3 && b4) {
JOptionPane.showMessageDialog(null, "IPAddress is exactly! ");
} else {
JOptionPane.showMessageDialog(null, "IPAddress is error ");
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
大体如下
-----------------------------------------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class demo {
public static void main(String[] args) {
String pattern = "(\\d{1,3}\\.){3}\\d{1,3}";
String str = "ip addess 192.160.2.3";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
String result = null;
while (m.find()) {
result = m.group();
if (!"".equals(result)) {
break;
}
}
System.out.println(result);
boolean isOK = true;
String[] array = result.split("\\.");
for (int i = 0; i < array.length; i++) {
int ip = Integer.parseInt(array[i]);
if (!(0 <= ip && ip <= 255)) {
isOK = false;
break;
}
}
if (isOK) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
-----------------------------------------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class demo {
public static void main(String[] args) {
String pattern = "(\\d{1,3}\\.){3}\\d{1,3}";
String str = "ip addess 192.160.2.3";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
String result = null;
while (m.find()) {
result = m.group();
if (!"".equals(result)) {
break;
}
}
System.out.println(result);
boolean isOK = true;
String[] array = result.split("\\.");
for (int i = 0; i < array.length; i++) {
int ip = Integer.parseInt(array[i]);
if (!(0 <= ip && ip <= 255)) {
isOK = false;
break;
}
}
if (isOK) {
System.out.println("OK");
} else {
System.out.println("NG");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上正解
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询