1

有一些 XML 解析的文本看起来像这样:

06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href="..."> ... <br>15:45 Something..

而且有很多..

好吧,我已经这样做了:

String mim =ses.replaceAll("(?s)\\<.*?\\>", " \n");

没有其他方法可以很好地显示文本。现在,经过几次展示,一段时间后,我需要将相同的文本分成单独的字符串,如下所示:

06:00 Vesti   

... 或者

07:15 Something Else

我已经尝试过这样的事情,但它不起作用:

char[] rast = description.toCharArray();
    int brojac = 0;
    for(int q=0; q<description.length(); q++){
        if(rast[q]=='\\' && rast[q+1]=='n' ) brojac++;
    }
    String[] niz = new String[brojac];

    int bf1=0;
    int bf2=0;
    int bf3=0;
    int oo=0;

    for(int q=0; q<description.length(); q++){
        if(rast[q]=='\\'&& rast[q+1]=='n'){
            bf3=bf1;
            bf1=q;

            String lol = description.substring(bf3, bf1);
            niz[oo]=lol;
            oo++;
        }
    }

我知道在 description.substring(bf3,bf1) 中没有按应有的方式设置,但我认为:

if(rast[q]=='\\' && rast[q+1]=='n) 

那样不行..还有其他解决方案吗?

笔记。没有其他方法可以获取该资源。,一定是通过这个。

4

2 回答 2

1

调用Html.fromHtml(String)将正确地转换<br>成 \n。

String html = "06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href=\"...\"> ... <br>15:45 Something..";
String str = Html.fromHtml(html).toString();
String[] arr = str.split("\n");

然后,只需按行拆分它 - 不需要正则表达式(在第一种情况下,您不应该使用它来解析 HTML)。

编辑:把所有东西都变成一堆Dates

// Used to find the HH:mm, in case the input is wonky
Pattern p = Pattern.compile("([0-2][0-9]:[0-5][0-9])");
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
SortedMap<Date, String> programs = new TreeMap<Date, String>();
for (String row : arr) {
    Matcher m = p.matcher(row);
    if (m.find()) {
        // We found a time in this row
        ParsePosition pp = new ParsePosition(m.start(0));
        Date when = fmt.parse(row, pp);
        String title = row.substring(pp.getIndex()).trim();
        programs.put(when, title);
    }
}
// Now programs contain the sorted list of programs. Unfortunately, since
// SimpleDateFormat is stupid, they're all placed back in 1970 :-D.
// This would give you an ordered printout of all programs *AFTER* 08:00
Date filter = fmt.parse("08:00");
SortedMap<Date, String> after0800 = programs.tailMap(filter);
// Since this is a SortedMap, after0800.values() will return the program names in order.
// You can also iterate over each entry like so:
for (Map.Entry<Date,String> program : after0800.entrySet()) {
    // You can use the SimpleDateFormat to pretty-print the HH:mm again.
    System.out.println("When:" + fmt.format(program.getKey()));
    System.out.println("Title:" + program.getValue());            
}
于 2012-08-16T13:41:59.177 回答
0

使用正则表达式:

List<String> results = new ArrayList<String>(); 

Pattern pattern = Pattern.compile("(\d+:\d+ \w+)<?");
Matcher matcher = pattern.matcher("06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href="..."> ... <br>15:45 Something..");

while(matcher.find()) {
    results.add(matcher.group(0));
}

results将作为字符串列表结束:

results = List[
    "06:00 Vesti", 
    "07:15 Something Else", 
    "09:10 Movie", 
    "15:45 Something.."]

请参阅Rexgex Java 教程,了解 Java 正则表达式库的工作原理。

于 2012-08-16T13:26:24.363 回答