这题用来练正则不错,果断用JAVA搞了,正则还是不大熟,多练练才会有进步
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args)
{
String pattern = "(http|ftp|gopher)://([^:/]+)(:\\d+)?(/\\S+)?";
Pattern p = Pattern.compile(pattern);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 1; i <= n; i++)
{
System.out.format("URL #%d\n", i);
Matcher m = p.matcher(sc.nextLine());
if (m.find())
{
System.out.format("Protocol = %s\n", m.group(1));
System.out.format("Host = %s\n", m.group(2));
System.out.format("Port = %s\n",
m.group(3) == null ? "<default>" : m.group(3)
.substring(1));
System.out.format("Path = %s\n",
m.group(4) == null ? "<default>" : m.group(4)
.substring(1));
}
System.out.println();
}
sc.close();
}
}