2012-12-14 39 views
-4

我試圖解析一個簡單的<font>標籤的樣式屬性,並將其轉換回簡單的HTML屬性。舉例來說,我有這個字符串<font style="font-family:tahoma;font-size:24px;color:#9900CC;">,我想以某種方式將其轉換爲<font size="24" color="#9900CC" face="tahoma">我知道這可以用正則表達式來完成,但我不知道怎麼樣?簡單的CSS屬性的HTML解析器與Java

感謝

+0

爲什麼你需要嗎? –

+0

如果你知道如何使用正則表達式,你爲什麼不使用正則表達式? –

+0

@JanDvorak你是否假設'replaceAll'不使用正則表達式? – melpomene

回答

0

因此,這裏是我到目前爲止做出了一個骯髒的解決方案最簡單的解決方案,但它就像一個魅力:

static public String convertCSSFonttoHTML(String css) 
{ 

    List<List<String>> allFontTags = regexFindMultiStrings("<font[^>]*(style=['\"][^'\"]+['\"])[^>]*>", css); 
    String allAttributes = ""; 

    for(int y=0; y<allFontTags.size(); y++) 
    { 
     String style = allFontTags.get(y).get(0); 
     String size = regexFindString("font-size:([^0-9]+)", style); 
     String color = regexFindString("color:([^;]+);", style); 
     String face = regexFindString("font-family:([^;]+)", style); 

     if(!size.isEmpty()) 
      allAttributes += "size=\""+size+"\" "; 

     if(!color.isEmpty()) 
      allAttributes += "color=\"" + color + "\" "; 

     if(!size.isEmpty()) 
      allAttributes += "face=\""+face+"\""; 



     //do replacements to the first occurance 
     css = css.replaceFirst(style, allAttributes); 

     //empty atts 
     allAttributes = ""; 
    } 

    //Log.e("Regex", css); 


    return css; 

}