Java tutorial
//package com.java2s; //License from project: Apache License import java.util.regex.Matcher; import java.util.regex.Pattern; import android.graphics.drawable.Drawable; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ImageSpan; public class Main { public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern, Drawable drawable) { SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence : new SpannableString(charSequence); try { ImageSpan is = new ImageSpan(drawable); Pattern pattern = Pattern.compile(regPattern); Matcher matcher = pattern.matcher(ss); while (matcher.find()) { String key = matcher.group(); ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } } catch (Exception ex) { // Logger.e(TAG, ex); } return ss; } }