简体   繁体   中英

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

I tried the Html.fromhtml and use a custom taghandler, but it seems only support custom tags starts with "<" and ends ">" Any suggestions?

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):

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. You can just use String.replace() for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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