is it possible to find a #hashtag and an "http://" link from a string and color it ? I am using this in Android.
public void setTitle(String title) {
this.title = title;
}
Thanks in Advance
I have found an answer and here is the way to do it :
SpannableString hashtagintitle = new SpannableString(imageAndTexts1.get(position).getTitle());
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashtagintitle);
while (matcher.find())
{
hashtagintitle.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);
}
textView.setText(hashtagintitle);
if you want to divide COMPOUND WORD into its parts.
from
to
You can use this code below.
public static List<String> getHashTags(String str) {
Pattern MY_PATTERN = Pattern.compile("(#[a-zA-Z0-9ğüşöçıİĞÜŞÖÇ]{2,50}\\b)");
Matcher mat = MY_PATTERN.matcher(str);
List<String> strs = new ArrayList<>();
while (mat.find()) {
strs.add(mat.group(1));
}
return strs;
}
This function will return a list of all the #hashtags in your string
public static List<String> getHashTags(String str) {
Pattern MY_PATTERN = Pattern.compile("#(\\S+)");
Matcher mat = MY_PATTERN.matcher(str);
List<String> strs = new ArrayList<>();
while (mat.find()) {
strs.add(mat.group(1));
}
return strs;
}
You could match it using a regex.
Heres the Class in Android for using Regexes.