1

所以我的目标是创建一个 URL 缩短器,它可以正常工作,除非我在一行中输入两个 URL。

例如,如果我输入“laskjdflas www.google.com lakdsjfsa www.google.ca”,我会得到以下响应:

请输入要缩短的网址

laskjdf www.google.ca lksadjf www.google.com

laskjdf http://aman207.tk/9 lksadjf http://aman207.tk/9

laskjdf htt://aman207.tk/-4gi5 lksadjf htt://aman207.tk/-4gi5

(我知道最后两个链接缺少 ap)

这是我的代码:

Scanner keyboard=new Scanner(System.in);
System.out.println("Please enter in a URL to shorten");
URLget=keyboard.nextLine();
String originalMessage=URLget;

Pattern p = Pattern.compile("(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");
Matcher m = p.matcher(URLget);
StringBuffer sb = new StringBuffer();
while (m.find())
{
   URLget=m.group(1);
   m.appendReplacement(sb, "");
   sb.append(URLget);
   m.appendTail(sb);
   String URL="http://www.aman207.tk/yourls-api.php?signature=0a88314b95&action=shorturl&url="+ URLget;
   if (URLget.startsWith("http://")||URLget.startsWith("www."))
   {
       try {
           DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
           Document doc = docBuilder.parse(new InputSource(new URL(URL).openStream()));

           NodeList nodeList = doc.getElementsByTagName("shorturl");

           for (int temp = 0; temp < nodeList.getLength(); temp++)
           {
               Node nNode = nodeList.item(temp);
               Element eElement = (Element) nNode;
               if(eElement.getAttribute("shorturl") != null)
               {
                   String findShortURL= eElement.getTextContent();
                   String finalMessage = originalMessage.replaceAll("(?:http://|www.?)[\\w/%.-]+", findShortURL);
                   System.out.println(finalMessage);
               }
            }
        }
    }
}

我需要做的是,它在一行上替换每个 URL。有人有什么建议吗?谢谢!

编辑:

输入:随机词 [要缩短的 URL (URL 1 )] 更多随机词 [要缩短的 URL (URL 2 )]

输出:

相同的随机词 [Shortened URL 1 ] 相同的随机词 [Shortened URL 1(它与第一个 URL 是相同的缩短 URL。我需要它像预期的输出)]

预期输出:

相同的随机词 [Shortened URL 1 ] 相同的随机词 [Shortened URL 2 ]

4

2 回答 2

1

用这个替换你的if声明:

if(eElement.getAttribute("shorturl") != null)
{                      
    String findShortURL= eElement.getTextContent();
    originalMessage = originalMessage.replaceAll(URLget, findShortURL);
    System.out.println(originalMessage);
}

println在循环外使用for它来给你一次输出。

于 2013-06-12T05:37:37.503 回答
0

我自己想通了。

这是工作代码

Pattern p = Pattern.compile("(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");
Matcher m = p.matcher(URLget);
StringBuffer sb = new StringBuffer();  
while (m.find())  
     {  
        URLget=m.group(1);  
        String URL="http://www.aman207.tk/yourls-api.php?signature=0a88314b95&action=shorturl&url="+ URLget;
        if (URLget.startsWith("http://")||URLget.startsWith("www."))
    {
        try {               
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new InputSource(new URL(URL).openStream()));

            NodeList nodeList = doc.getElementsByTagName("shorturl");

            for (int temp = 0; temp < nodeList.getLength(); temp++) {

                Node nNode = nodeList.item(temp);
                Element eElement = (Element) nNode;
                if(eElement.getAttribute("shorturl") != null)
                {
                    URLget=eElement.getTextContent();

                }
                else
                {

                }

            }

    }

       catch (IOException e) {
        e.printStackTrace();
        System.err.println("Error occured");
    }  catch (SAXException e) {
        System.err.println("You either entered in an invalid URL, or our URL shortener services are down. Please try again.");
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    }
    else
    {

    }
    m.appendReplacement(sb, "");
    sb.append(URLget);

     }
    m.appendTail(sb);
    return (sb.toString());
于 2013-06-12T21:03:42.410 回答