简体   繁体   English

Android:解析自定义论坛标签

[英]Android:Parse custom forum Tags

I want to parse some strings which contains custom forum tags in them,like: 我想解析一些包含自定义论坛标签的字符串,例如:

[url=https://play.google.com/store/apps/details?id=xxx]----sent from my Sony Ericsson LT28h,Android 4.2.1[/url]

and show this tag string in a textview to be a clickable link ----sent from my Sony Ericsson LT28h,Android 4.2.1 并在textview中将此标记字符串显示为可点击链接----从我的索尼爱立信LT28h,Android 4.2.1发送

I tried the Html.fromhtml and use a custom taghandler, but it seems only support custom tags starts with "<" and ends ">" Any suggestions? 我尝试了Html.fromhtml并使用自定义标签处理程序,但它似乎只支持自定义标签以“<”开头并结束“>”任何建议?

one way is to write a custom handler for these, check for a sample code below, it may give you a hint (note this code will return you inner value of [url]...[/url] tags you need to create one more iteration for [url=....] tag using same logic to get url's value): 一种方法是为这些编写自定义处理程序,检查下面的示例代码,它可能会给你一个提示(注意这段代码将返回你需要创建一个[url] ... [/ url]标签的内部值[url = ....]标记的更多迭代使用相同的逻辑来获取url的值):

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyHandler {

    public static void main(String[] args) {
        Pattern p = Pattern.compile(
                "\[url.*\](.*)\[/url\]",
                Pattern.DOTALL
            );

        Matcher matcher = p.matcher(
                "[url=https://play.google.com/store/apps/details?id=xxx]----sent from my Sony Ericsson LT28h,Android 4.2.1[/url]"
            );

        if(matcher.matches()){
            System.out.println(matcher.group(1));
        }
    }
}

You can't expect a general method to know all possible custom tags which forums come up with of course. 当然,您不能指望一种通用方法来了解论坛提出的所有可能的自定义标记。 So you'll need to do it manually. 所以你需要手动完成。

I guess your best bet is to set up a method where you replace the custom tags with their 'official' HTML tag, if they have one. 我想你最好的办法是设置一种方法,用自己的'官方'HTML标签替换自定义标签,如果有的话。 You can just use String.replace() for this. 你可以使用String.replace()来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM