private static void replaceControlCodes(Matcher m,SpannableStringBuilder ssb,CharacterStyle style){
  ArrayList<Integer> toremove=new ArrayList<Integer>();
  while (m.find()) {
    toremove.add(0,m.start());
    if (m.group(2) != null && m.group(2) != m.group(3)) {
      toremove.add(0,m.end() - 1);
    }
    ssb.setSpan(style,m.start(),m.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  for (  Integer i : toremove) {
    ssb.delete(i,i + 1);
  }
}

private static void replaceControlCodes ( Matcher m , SpannableStringBuilder ssb , CharacterStyle style ) { ArrayList < Integer > toremove = new ArrayList < Integer > ( ) ; while ( m . find ( ) ) { toremove . add ( 0 , m . start ( ) ) ; if ( m . group ( 2 ) != null && m . group ( 2 ) != m . group ( 3 ) ) { toremove . add ( 0 , m . end ( ) - 1 ) ; } ssb . setSpan ( style , m . start ( ) , m . end ( ) , Spannable . SPAN_EXCLUSIVE_EXCLUSIVE ) ; } for ( Integer i : toremove ) { ssb . delete ( i , i + 1 ) ; } }
replace the control codes
