2013-06-12 14 views
1

所以我的目標是創建一個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

(我知道這最後兩個鏈接都缺少P)

這是我的代碼:

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 )]更隨機字[URL縮短(URL )]

輸出:

相同的隨機單詞[縮短的網址]相同的隨機單詞[縮短的網址(它與第一個URL縮短的URL相同,我需要它像預期的輸出)

預期輸出:

相同的隨機字[縮短的URL ]相同的隨機字[縮短的URL ]

+2

請澄清,輸入,輸出和期望輸出 –

+0

我編輯我原來的職位 – aman207

+0

也許稍微偏離主題,但你從哪裏得到的是正則表達式解析URL? –

回答

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()); 
1

這個替換您if聲明:

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

for循環之外使用println來讓它輸出一次。