Android SpannableStringBuilder支持多种模式

Android SpannableStringBuilder支持多种模式,android,regex,spannablestring,spannable,mention,Android,Regex,Spannablestring,Spannable,Mention,我为不同的实体(产品、房间、帖子和用户)提供了不同的屏幕。 它们都有不同的ID,我使用该ID打开特定屏幕,尝试从通过API传递到相关屏幕/活动的ID参数加载数据 我特别想到的产品、房间和帖子的机制是,在通知屏幕和类似屏幕上,我想发送编码或标记的字符串;例如,产品如下所示: You have bid on <p244 24LED Lampshade /> 你已经出价了 为了与上面的匹配,我有一个正则表达式: <p([0-9]*)\s(.*?)\/>(\s|$) (\s

我为不同的实体(产品、房间、帖子和用户)提供了不同的屏幕。 它们都有不同的ID,我使用该ID打开特定屏幕,尝试从通过API传递到相关屏幕/活动的ID参数加载数据

我特别想到的产品、房间和帖子的机制是,在通知屏幕和类似屏幕上,我想发送编码或标记的字符串;例如,产品如下所示:

You have bid on <p244 24LED Lampshade />
你已经出价了
为了与上面的匹配,我有一个正则表达式:

<p([0-9]*)\s(.*?)\/>(\s|$)
(\s |$)
下面是一个示例字符串:

 String dummyText = "Hi @dan we product test <p1337 product />" +
                        "\n how about <r123 Room name/> is a nice room" +
                        "\n what <t234 this post details more about it/> but you should " +
                        "\n #poprocks woah one #sad";
String dummyText=“Hi@dan we产品测试”+
“\n一间漂亮的房间怎么样”+
“\n但是你应该”+
“\n#爆米花woah one#sad”;
单次使用product/post/room似乎可以很好地工作,但它会弄乱更多的编码/标记字符串实例

但正如我所说,有多个实例,如以下测试:

 String dummyText = "Hi @dan we product test <p1337 product /> and were more happy with <p53 car/> " +
                        "\n eh <r123 Room name/> is a nice room and <r233 dans house/> sucked while <r123 fun time/> was ok " +
//                        "\n what <t234 this post details more about it/> but you should " +
                        "\n <t234 view this as well/>" +
                        "\n #poprocks woah one #sad";
String dummyText=“Hi@dan我们对产品进行了测试,并且更满意”+
“\n eh是一个很好的房间,虽然还可以,但很糟糕”+
//“\n但是你应该”+
“\n”+
“\n#爆米花woah one#sad”;
它搞砸了:

以下是我的整个代码流程:

String dummyText = "Hi @dan we product test <p1337 product /> and were more happy with <p53 car/> " +
                        "\n eh <r123 Room name/> is a nice room and <r233 dans house/> sucked while <r123 fun time/> was ok " +
//                        "\n what <t234 this post details more about it/> but you should " +
                        "\n <t234 view this as well/>" +
                        "\n #poprocks woah one #sad";

                List<Pattern> patternsList = new ArrayList<>();
                patternsList.add(Pattern.compile(REGEX_NOTI_ROOMS)); //0
                patternsList.add(Pattern.compile(REGEX_NOTI_PRODUCTS)); //1
                patternsList.add(Pattern.compile(REGEX_NOTI_TWEETS)); //2
                patternsList.add(Pattern.compile(REGEX_NOTI_MENTION)); //3
                patternsList.add(Pattern.compile(REGEX_ARABIC_N_NUM_HASHTAG)); //4
                holder.row_noti_messageTV.setText(makeSpannable(dummyText, patternsList));
                holder.row_noti_messageTV.setMovementMethod(new PkMovementMethod());
String dummyText=“Hi@dan我们对产品进行了测试,并且更满意”+
“\n eh是一个很好的房间,虽然还可以,但很糟糕”+
//“\n但是你应该”+
“\n”+
“\n#爆米花woah one#sad”;
列表模式列表=新的ArrayList();
patternsList.add(Pattern.compile(REGEX_NOTI_房间))//0
patternsList.add(Pattern.compile(REGEX_NOTI_产品))//1.
patternsList.add(Pattern.compile(REGEX_NOTI_TWEETS))//2.
patternsList.add(Pattern.compile(REGEX_NOTI_anti_))//3.
patternsList.add(Pattern.compile(REGEX_ARABIC_N_NUM_HASHTAG))//4.
holder.row_noti_messageTV.setText(makeSpannable(dummyText,patternsList));
holder.row_noti_messageTV.setMovementMethod(新的PkMovementMethod());
如果相关正则表达式为:

    public static final String REGEX_NOTI_ROOMS ="<r([0-9]*)\\s(.*?)\\/>(\\s|$)";
    public static final String REGEX_NOTI_PRODUCTS ="<p([0-9]*)\\s(.*?)\\/>(\\s|$)";
    public static final String REGEX_NOTI_TWEETS ="<t([0-9]*)\\s(.*?)\\/>(\\s|$)";
    public static final String REGEX_ARABIC_N_NUM_HASHTAG ="#(\\w*[0-9a-zA-Zء-ي٠-٩]+\\w*[0-9a-zA-Zء-ي٠-٩])";
    public static final String REGEX_NOTI_MENTION ="(?:^|\\s|$|[.])@[\\p{L}0-9_]*";
公共静态最终字符串REGEX_NOTI_ROOMS=“(\\s |$)”;
公共静态最终字符串REGEX_NOTI_PRODUCTS=“(\\s|$)”;
公共静态最终字符串REGEX_NOTI_TWEETS=“(\\s |$)”;
公共静态最终字符串REGEX_ARABIC_N_NUM_HASHTAG=“#”(\\w*[0-9a-zA-Zء-ي-٠]+\\w*[0-9a-zA-Zء-ي-٠]”;
公共静态最终字符串REGEX_NOTI_antie=“(?:^ |\\s |$|[.])@[\\p{L}0-9\]*”;
我的makespanable方法是:

    public SpannableStringBuilder makeSpannable(String rawText, List<Pattern> listofPatterns) {

//        StringBuffer sb = new StringBuffer();
        SpannableStringBuilder spannable = new SpannableStringBuilder(rawText);

        for(int i=0; i<listofPatterns.size(); i++)
        {
            Matcher matcher = null;
            if(i==0) //init only
                matcher = listofPatterns.get(i).matcher(rawText);
            else
                matcher = listofPatterns.get(i).matcher(spannable);

            while (matcher.find()) {
                showLogMessage("jsonParse", "hit on iteration" + i + " group == " + matcher.group());
                if(i==3 || i == 4)
                {
                    try {
                        String abbr = matcher.group();
                        showLogMessage("jsonParse", "loop[3 | 4 are normal] == " + i + " group(0) == " + abbr);

                        int start = matcher.start();
                        int end = matcher.end();

                        NotificationSpan ourSpan = new NotificationSpan(Color.BLUE, Color.RED, Color.TRANSPARENT, new IdTitleStore(abbr, abbr, abbr), NotificationAdapter.this);
                        spannable.setSpan(ourSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else if(i<=2) {
                    try {
                        String orgGroup = matcher.group();
                        // get the match
                        String abbr = matcher.group(2);
                        showLogMessage("jsonParse", "loop == " + i + " group2 == " + abbr);

                        int startPoint = matcher.start();
                        int endPoint = matcher.end();

                        NotificationSpan ourSpan = new NotificationSpan(Color.RED, Color.BLUE, Color.TRANSPARENT, new IdTitleStore(matcher.group(1), abbr, orgGroup), NotificationAdapter.this);
                        Spannable spanText = new SpannableString(abbr);
                        spanText.setSpan(
                                ourSpan, 0, abbr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                        );

                        spannable.replace(startPoint, endPoint, spanText);


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }

        return spannable;
    }
public SpannableStringBuilder makeSpannable(字符串rawText,模式列表){
//StringBuffer sb=新的StringBuffer();
SpannableStringBuilder spannable=新的SpannableStringBuilder(原始文本);

对于(int i=0;我认为您需要从所有正则表达式中删除
(\s |$)
。它看起来是冗余的。嗯。从所有正则表达式中删除冗余部分似乎没有任何作用。更新日志的post
String dummyText = "Hi @dan we product test <p1337 product /> and were more happy with <p53 car/> " +
                        "\n eh <r123 Room name/> is a nice room and <r233 dans house/> sucked while <r123 fun time/> was ok " +
                        "\n what <t233 this post details more about it/> but you should " +
                        "\n <t234 view this as well/>" +
                        "\n #poprocks woah one #sad";

E/jsonParse: hit on iteration0 group(0) == <r123 Room name/>
E/jsonParse: hit on iteration0 group(0) == <r233 dans house/>
E/jsonParse: hit on iteration0 group(0) == <r123 fun time/>
E/jsonParse: hit on iteration1 group(0) == <p1337 product />
E/jsonParse: hit on iteration1 group(0) == <p53 car/>
E/jsonParse: hit on iteration2 group(0) == <t234 view this as well/>
E/jsonParse: hit on iteration3 group(0) ==  @dan
E/jsonParse: hit on iteration4 group(0) == #poprocks
E/jsonParse: hit on iteration4 group(0) == #sad

"\n what <t234 this post details more about it/> but you should "
public SpannableStringBuilder makeSpannable(String rawText, List<Pattern> listofPatterns) {

        SpannableStringBuilder spannable = new SpannableStringBuilder(rawText);

        Matcher matcher = null;
        for(int i=0; i<listofPatterns.size(); i++)
        {
            matcher = listofPatterns.get(i).matcher(spannable.toString());

            while (matcher.find()) {
                showLogMessage("jsonParse", "hit on iteration" + i + " group == " + matcher.group());
                if(i<=2) {
                    try {
                        String orgGroup = matcher.group();
                        // get the match
                        String abbr = matcher.group(2);
                        showLogMessage("jsonParse", "span txt of iteration " + i + " going to be group2 == " + abbr);

                        int startPoint = matcher.start();
                        int endPoint = matcher.end();

                        NotificationSpan ourSpan = new NotificationSpan(Color.RED, Color.BLUE, Color.TRANSPARENT, new IdTitleStore(matcher.group(1), abbr, orgGroup), NotificationAdapter.this);
                        Spannable spanText = new SpannableString(abbr);
                        spanText.setSpan(
                                ourSpan, 0, abbr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                        );

                        spannable.replace(startPoint, endPoint-1, spanText);
                        matcher = listofPatterns.get(i).matcher(spannable);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else {
                    try {
                        String abbr = matcher.group();
                        showLogMessage("jsonParse", "span txt of iteration " + i + " going to be group(0) == " + abbr);

                        int start = matcher.start();
                        int end = matcher.end();

                        NotificationSpan ourSpan = new NotificationSpan(Color.BLUE, Color.RED, Color.TRANSPARENT, new IdTitleStore(abbr, abbr, abbr), NotificationAdapter.this);
                        spannable.setSpan(ourSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return spannable;

    }