Cómo colorear texto utilizando Regex en android

Tengo tres expresiones regulares:

Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)"); Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)"); Pattern urlPattern = Patterns.WEB_URL; 

Tengo una cadena:

Este es un #sample #twitter texto de @tom_cruise con un enlace http://tom_cruise.me

Necesito emparejar este texto con la expresión regular tres arriba y colorear el texto emparejado con azul y fijar el texto final en un TextView . ¿Cómo puedo lograrlo?

Hay que mencionar que no necesito Linkify el texto, sólo color. Y no estoy usando Twitter4j Library.

He reemplazado http://tom_cruise.me con http://www.google.com . Pruebe lo siguiente:

 String a = "This is a #sample #twitter text of @tom_cruise with a link http://www.google.com"; Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)"); Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)"); Pattern urlPattern = Patterns.WEB_URL; StringBuffer sb = new StringBuffer(a.length()); Matcher o = hashtagPattern.matcher(a); while (o.find()) { o.appendReplacement(sb, "<font color=\"#437C17\">" + o.group(1) + "</font>"); } o.appendTail(sb); Matcher n = mentionPattern.matcher(sb.toString()); sb = new StringBuffer(sb.length()); while (n.find()) { n.appendReplacement(sb, "<font color=\"#657383\">" + n.group(1) + "</font>"); } n.appendTail(sb); Matcher m = urlPattern.matcher(sb.toString()); sb = new StringBuffer(sb.length()); while (m.find()) { m.appendReplacement(sb, "<font color=\"#EDDA74\">" + m.group(1) + "</font>"); } m.appendTail(sb); textView.setText(Html.fromHtml(sb.toString())); 

Eche un vistazo a SpannableString y SpannableStringBuilder . Un ejemplo de uso de SpannableStringBuilder está disponible en https://stackoverflow.com/a/16061128/1321873

Podrías escribir un método que acepte la String sin estilo y devuelve un CharSequence así:

 private CharSequence getStyledTweet(String tweet){ SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tweet); //Find the indices of the hashtag pattern, mention pattern and url patterns //and set the spans accordingly //... return stringBuilder; } 

y luego utilice el valor devuelto de lo anterior para establecer el texto de TextView

 TextView tView = (TextView)findViewById(R.id.myText); tView.setText(getStyledTweet(tweet)); 
  • ¿Cómo puedo crear una expresión regular para esto en android?
  • División de la cadena con RegEx en Android
  • Java no funciona con regex \ s, dice: inválido secuencia de escape
  • Ant regex para nombres de paquetes android
  • Divide la cadena en la última aparición del carácter
  • ¿Hay una manera rápida de reconocer códigos HTML ASCII en una cadena o TextView?
  • Android, String.split (String regex) no divide toda la cadena
  • Android / Java regex match Más signo
  • Expresión regular de Android: devuelve la cadena correspondiente
  • Android / Java Regex para eliminar ceros adicionales de sub-cadenas
  • Consulta de SQLiteDatabase de Android con Regex
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.