日期:2014-05-20  浏览次数:20988 次

求一正则表达式
例如:
字符串如下:
12.24.45.100:80   HTTP   210,410,420   US   ATT   User  
12.214.227.97:2550   HTTP   270,821,821   US   ATT   User  
24.166.145.107:2442   HTTP   231,901,901   美国   Road  
61.26.29.48:8080   HTTP   80,761,781   Japan  

想把   IP地址和端口号取出来

12.24.45.100:80
12.214.227.97:2550
24.166.145.107:2442
61.26.29.48:8080

求正则表达式或者可实现的方法

------解决方案--------------------
\d{1,3}(\.\d{1,3}){3}:\d{1,4}
------解决方案--------------------
ding ls
这样效率会好点
\d{1,3}(?:\.\d{1,3}){3}:\d{1,4}

------解决方案--------------------
public class Test {
public static void main(String[] args) {
String s = "12.24.45.100:80 HTTP 210,410,420 US ATT User 12.214.227.97:2550 HTTP 270,821,821 US ATT User 24.166.145.107:2442 HTTP 231,901,901 美国 Road 61.26.29.48:8080 HTTP 80,761,781 Japan ";
String[] array = s.split( "HTTP ");
for (int i = 0; i < array.length; i++) {
if (array[i].indexOf( ": ") != -1) {
int p = array[i].indexOf( ": ");
int begin = getBegin(array[i], p);
int end = getEnd(array[i], p);
System.out.println(array[i].substring(begin, end));
}
}
}

private static int getBegin(String s, int p) {
char c = 32;
int b = p;
do {
if (b <=0) break;
c = s.charAt(b--);
} while (c != 32);
if (b == 0)
return 0;
else
return ++b;
}

private static int getEnd(String s, int p) {
char c = 32;
int b = p;
do {
c = s.charAt(b++);
} while (c != 32);

return b--;
}

}
------解决方案--------------------
我用的split写的:

public class sql {
public static void main(String[] args) {
String str = "12.24.45.100:80 HTTP 210,410,420 US ATT User 12.214.227.97:2550 HTTP 270,821,821 US ATT User 24.166.145.107:2442 HTTP 231,901,901 美国 Road 61.26.29.48:8080 HTTP 80,761,781 Japan ";
String[] ip=str.split( "HTTP\\s(\\d+,?)+\\D+ ");
System.out.println(ip.length);

for (int i=0;i <ip.length;i++)
{
System.out.println(ip[i]);
}
}
}

------解决方案--------------------
//正则表达式:

import java.util.regex.*;

public class Chazhao {

public static void main(String[] args) {
String s= "12.24.45.100:80 HTTP 210,410,420 US AT " +
"T User12.214.227.97:2550 " +
"HTTP 270,821,821 US ATT User 24.166.145.107:2442 " +
"HTTP 231,901,901 美国 Road 61.26.29.48:8080 " +
"HTTP 80,761,781 Japan ";
Pattern p = Pattern.compile( "(\\d+\\.){3}\\d+\\:\\d+ ");
Matcher m = p.matcher(s);
while(m.find()){
System.out.print( "HTTP:// ");
System.out.println(m.group());
}

}

}