class TextUtils {

    public static String replaceFirst( String string, final String regex, final String replacement, final int occurrences )
    {
        StringBuffer sb = new StringBuffer();
        Matcher matcher = Pattern.compile( regex ).matcher( string );
        int c = 0;

        while ( matcher.find() && c < occurrences )
        {
            matcher.appendReplacement( sb, replacement );
            c++;
        }

        return appendTail( matcher, sb );
    }

}