Android-Weibo short link regular Expressions and special text highlighting

Source: Internet
Author: User

Sina Weibo third-party clients, the text content of special characters (such as Web page short link, # #话题, @ person name , etc.) to highlight color, need to use the following several regular expressions.

  

1 /**Short link Regular expression for Weibo*/2      Static FinalString regex_http = "http (s)?:/ /([a-za-z|\\d]+\\.) +[a-za-z|\\d]+ (/[a-za-z|\\d|\\-|\\+|_./?%=]*)? ";3      Static FinalString regex_at = "@[\\u4e00-\\u9fa5\\w\\-]+";4      Static FinalString regex_sharp= "# ([^\\#|.] +)#";5      Static FinalString regex_emoji= "\\[[a-za-z0-9\\u4e00-\\u9fa5]+\\]";

Next is the function highlighting the special field, in a previous essay "Android-textview keyword highlighting and hyperlinks" in the example, this example will be modified to make it suitable for the use of this project.

1 /**2 * Highlight some text3      * @paramContent Text Contents4      * @return5      */6      Public Staticspannablestring settexthighlight (String content) {7spannablestring result =Newspannablestring (content);8 9         if(Content.contains ("@")) {TenPattern p =Pattern.compile (regex_at); OneMatcher m =P.matcher (result); A              while(M.find ()) { -                 intStart =M.start (); -                 intEnd =m.end (); the Result.setspan ( -(NewForegroundcolorspan (Color.parsecolor ("#33b5e5"))), - start, end, spannable.span_exclusive_inclusive); -             }                                     +         } -          +         if(Content.contains ("#") ){ APattern p =Pattern.compile (regex_sharp); atMatcher m =P.matcher (result); -              while(M.find ()) { -                 intStart =M.start (); -                 intEnd =m.end (); - Result.setspan ( -(NewForegroundcolorspan (Color.parsecolor ("#ff7d00"))), in start, end, spannable.span_exclusive_inclusive); -             } to         } +          -         if(Content.contains ("HTTP//") ){ thePattern p =Pattern.compile (regex_http); *Matcher m =P.matcher (result); $              while(M.find ()) {Panax Notoginseng                 intStart =M.start (); -                 intEnd =m.end (); the Result.setspan ( +(NewForegroundcolorspan (Color.parsecolor ("#33b5e5"))), A start, end, spannable.span_exclusive_inclusive); the             } +         } -          $         returnresult; $}

The following is the application of the above method

1 spannablestring text = utils.settexthighlight (Weibo.gettext ()); 2         Holder.content.setText (text);

As follows

  

  

Nexus5 screenshot Small Tips:

Simultaneously press "Volume Down" and "power key" can quickly screenshot, together press these two keys 2 seconds to fix.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.