Android的匹配器和模式从链路切碎 [英] Android Matcher and Pattern cut up from the link
问题描述
我有一个包含一个链接字符串。
前的字符串:
我们喜欢吃巧克力,吃一上来在 http://t.co/9BDZvcx59d 。
块引用>所以,如果我显示这个字符串,因为它是,这将是相同的。但是,如果使用匹配和模式检测链路和颜色,它会砍碎。
它会是:大胆的绿色的
我们喜欢吃巧克力,吃一上来在 http://t.co
块引用>模式URLPATTERN = Patterns.WEB_URL; 匹配器M = urlPattern.matcher(sb.toString());
SB =新的StringBuffer(sb.length()); 而(m.find()){
m.appendReplacement(SB,&下;字体颜色= \\#006600 \\>中+ m.group(1)+&下; /字体>中);
}
m.appendTail(某人);我使用也试过
模式linkPattern = Pattern.compile((HTTP [A-ZA-Z0-9 _- +));
但无法插入://或在[]
解决方案您可以试试这个你的第二个方法。
模式linkPattern = Pattern.compile((http://t.co/[A-Za-z0-9_-]+));
在
t.co /
所以它不会面对符号或什么,但字母和数字,它将编译。I have a string that contains a link.
ex. of the string:
We love to eat choco, eat one up at http://t.co/9BDZvcx59d.
So If I display this string as it is, it would be the same. But if I use matcher and pattern to detect the link and color it, it will cut it up.
It'll be: Bold is green color.
We love to eat choco, eat one up at http://t.co.
Pattern urlPattern = Patterns.WEB_URL; Matcher m = urlPattern.matcher(sb.toString()); sb = new StringBuffer(sb.length()); while (m.find()) { m.appendReplacement(sb, "<font color=\"#006600\">" + m.group(1) + "</font>"); } m.appendTail(sb);
I also tried using
Pattern linkPattern = Pattern.compile("(http[A-Za-z0-9_-+)");
But couldn't insert : or // in the [].
解决方案You can try this for your second method.
Pattern linkPattern = Pattern.compile("(http://t.co/[A-Za-z0-9_-]+)");
It will compile after the
t.co/
so it won't face symbols or anything but letter and numbers.这篇关于Android的匹配器和模式从链路切碎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!