This works but I don't think it's efficient as it will have to iterate through the whole string. An i'll need to do it for alot of tweets...
Is there a better and more efficient way?
int atStart = 0;
int atEnd = 0;
boolean atFound = false;
String regex = "\\W";
System.out.println(str.length());
for(int i = 0;i < str.length();i++)
{
String a = Character.toString(str.charAt(i));
if(a.matches(regex)| i==str.length()-1 && atFound)
{
System.out.println(i + "REGEX MATCH");
if(i== str.length()-1)
{
atEnd = i+1;
}else
atEnd = i;
i--; // <- decrement. otherwise "@hello@hello" won't change
atFound = false;
str.setSpan(new BackgroundColorSpan(0xFFFF0000), atStart,
atEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else
if(a.equals("@"))
{
atStart = i;
atFound = true;
}
}