Common method used for processing the string to replace with emojis

@param text
@param regex
@return
550409
private static String processStringWithRegex(String text, Pattern pattern, int startIndex, boolean recurseEmojify) {
		//System.out.println(text);
		Matcher matcher = pattern.matcher(text);
		StringBuffer sb = new StringBuffer();
		int resetIndex = 0;
		
		if(startIndex > 0) {
			matcher.region(startIndex, text.length());
		} 
		
		while (matcher.find()) {
			
			String emojiCode = matcher.group();
			
			Emoji emoji = getEmoji(emojiCode);
			// replace matched tokens with emojis
			
			if(emoji!=null) {
				matcher.appendReplacement(sb, emoji.getEmoji());
			} else {
				if(htmlSurrogateEntityPattern2.matcher(emojiCode).matches()) {
					String highSurrogate1 = matcher.group("H1");
					String highSurrogate2 = matcher.group("H2");
					String lowSurrogate1 = matcher.group("L1");
					String lowSurrogate2 = matcher.group("L2");
					matcher.appendReplacement(sb, processStringWithRegex(highSurrogate1+highSurrogate2, shortCodeOrHtmlEntityPattern, 0, false));
					
					//basically this handles &#junk1;&#10084;&#65039;&#junk2; scenario
					//verifies if &#junk1;&#10084; or &#junk1; are valid emojis via recursion
					//if not move past &#junk1; and reset the cursor to &#10084;
					if(sb.toString().endsWith(highSurrogate2)) {
						resetIndex = sb.length() - highSurrogate2.length();
					} else {
						resetIndex = sb.length();
					}
					sb.append(lowSurrogate1);
					sb.append(lowSurrogate2);
					break;
				} else if(htmlSurrogateEntityPattern.matcher(emojiCode).matches()) {
					//could be individual html entities assumed as surrogate pair
					String highSurrogate = matcher.group("H");
					String lowSurrogate = matcher.group("L");
					matcher.appendReplacement(sb, processStringWithRegex(highSurrogate, htmlEntityPattern, 0, true));
					resetIndex = sb.length();
					sb.append(lowSurrogate);
					break;
				} else {
					matcher.appendReplacement(sb, emojiCode);
				}
			}

		}
		matcher.appendTail(sb);
		
		//do not recurse emojify when coming here through htmlSurrogateEntityPattern2..so we get a chance to check if the tail
		//is part of a surrogate entity
		if(recurseEmojify && resetIndex > 0) {
			return emojify(sb.toString(), resetIndex);
		}
		return sb.toString();
	}
912937
public <V> V convertFrom(final String text) {
        if (text == null) return null;
        return (V) convertFrom(Utility.charArray(text));
    }
208645
public static String parseFromUnicode(
    String input,
    EmojiTransformer transformer
  ) {
    int prev = 0;
    StringBuilder sb = new StringBuilder();
    List<UnicodeCandidate> replacements = getUnicodeCandidates(input);
    for (UnicodeCandidate candidate : replacements) {
      sb.append(input.substring(prev, candidate.getEmojiStartIndex()));

      sb.append(transformer.transform(candidate));
      prev = candidate.getFitzpatrickEndIndex();
    }

    return sb.append(input.substring(prev)).toString();
  }
840903
protected List<String> getTokens(String text){
		String[] tokens = text.toLowerCase().split("[\\s\\p{Punct}]");
		List<String> tokenList = new ArrayList<String>(tokens.length);
		for (int i = 0; i < tokens.length; i++){
			if (tokens[i].trim().length() < 1){
				continue;
			}
			tokenList.add(tokens[i].trim());
		}
		return tokenList;
	}
200209
def emojies_in(text)
      (sad_emojies + happy_emojies).map do |emoji|
        text.scan(emoji)
      end.flatten
    end
1279243
public static String get(String regex, CharSequence content, int groupIndex) {
		if (null == content || null == regex) {
			return null;
		}

		// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
		return get(pattern, content, groupIndex);
	}
296305
public Stream<HString> findAllPatterns(@NonNull TokenRegex regex) {
      return Streams.asStream(new Iterator<HString>() {
         TokenMatcher m = regex.matcher(HString.this);
         HString nextMatch = null;

         private boolean advance() {
            if (nextMatch == null && m.find()) {
               nextMatch = m.group();
            }
            return nextMatch != null;
         }

         @Override
         public boolean hasNext() {
            return advance();
         }

         @Override
         public HString next() {
            if (!advance()) {
               throw new NoSuchElementException();
            }
            HString toReturn = nextMatch;
            nextMatch = null;
            return toReturn;
         }
      });
   }
226619
public String replace(CharSequence text)
    {
        StringBuilder sb = new StringBuilder();
        replace(sb, text);
        return sb.toString();
    }
824972
public static SpannableString getEmojiString(Context context, String string, boolean adjustEmoji) {
        if (null == EmojiList) {
            initEmojiItems(context);
        }
        // 转换 Html，去掉两个表情之间的多个空格
        Spanned spanned = Html.fromHtml(string.replace("&nbsp;&nbsp;", ""));
        SpannableString spannableString = new SpannableString(spanned);
        // 通过传入的正则表达式来生成一个pattern
        Pattern emojiPatten = Pattern.compile(FACE_REGULAR, Pattern.CASE_INSENSITIVE);
        try {
            dealExpression(context, spannableString, emojiPatten, 0, adjustEmoji);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return spannableString;
    }
1055176
public static boolean isMatch(String regex, CharSequence content) {
		if (content == null) {
			// 提供null的字符串为不匹配
			return false;
		}

		if (StrUtil.isEmpty(regex)) {
			// 正则不存在则为全匹配
			return true;
		}

		// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
		return isMatch(pattern, content);
	}
730200
public static String simpleFormat (String text)
    {
        // first replace the image and other URLs
        Matcher m = URL_PAT.matcher(text);
        StringBuffer tbuf = new StringBuffer();
        while (m.find()) {
            String match = m.group();
            String lmatch = match.toLowerCase();
            if (lmatch.endsWith(".png") ||
                lmatch.endsWith(".jpg") ||
                lmatch.endsWith(".gif")) {
                match = "<img src=\"" + match + "\">";
            } else {
                match = "<a href=\"" + match + "\">" + match + "</a>";
            }
            m.appendReplacement(tbuf, match);
        }
        m.appendTail(tbuf);

        // then do our paragraph and list processing
        String[] lines = StringUtil.split(tbuf.toString(), "\n");
        StringBuilder lbuf = new StringBuilder();

        boolean inpara = false, inlist = false;
        for (String line2 : lines) {
            String line = line2;
            if (StringUtil.isBlank(line2)) {
                if (inlist) {
                    lbuf.append("</ul>");
                    inlist = false;
                }
                if (inpara) {
                    lbuf.append("</p>\n");
                    inpara = false;
                }
                continue;
            }

            if (!inpara) {
                inpara = true;
                lbuf.append("<p> ");
            }

            if (line.startsWith("*")) {
                if (inlist) {
                    lbuf.append("<li>");
                } else {
                    lbuf.append("<ul><li>");
                    inlist = true;
                }
                line = line.substring(1);
            }

            lbuf.append(line).append("\n");
        }

        if (inlist) {
            lbuf.append("</ul>");
        }
        if (inpara) {
            lbuf.append("</p>\n");
        }

        return lbuf.toString().trim();
    }
1046669
public String replace(CharSequence text) {
        TextBuffer tb = wrap(new StringBuilder(text.length()));
        replace(pattern.matcher(text), substitution, tb);
        return tb.toString();
    }
952532
public String replace(final String text) {
        
        if(Utils.isEmpty(text)) {
            return text;
        }
        
        final int length = text.length();
        StringBuilder replaced = new StringBuilder();
        int index = 0;
        
        while(index < length) {
            int multiIndex = replaceMulti(text, index, replaced);
            if(multiIndex > index) {
                index = multiIndex;
                continue;
            }
            
            int singleIndex = replaceSingle(text, index, replaced);
            if(singleIndex > index) {
                index = singleIndex;
                continue;
            }
            
            // 置換できるものがない場合
            replaced.append(text.charAt(index));
            index++;
            
        }
        
        return replaced.toString();
    }
1034893
async function replace(string, regex, callback) {
	regex.lastIndex = 0;

	/* get all data */
	const promises = [];
	string.replace(regex, (match, ...args) => {
		const promise = callback(match, ...args);
		promises.push(promise);
	});
	const data = await Promise.all(promises);

	/* replace all data */
	return data.length ? string.replace(regex, () => data.shift()) : string;
}
1247958
public static String removeAllEmojisExcept(
    String str,
    final Collection<Emoji> emojisToKeep
  ) {
    EmojiTransformer emojiTransformer = new EmojiTransformer() {
      public String transform(UnicodeCandidate unicodeCandidate) {
        if (emojisToKeep.contains(unicodeCandidate.getEmoji())) {
          return unicodeCandidate.getEmoji().getUnicode() +
            unicodeCandidate.getFitzpatrickUnicode();
        }
        return "";
      }
    };

    return parseFromUnicode(str, emojiTransformer);
  }
718602
public static Predicate<String> matchesRegex(final String regex) {
		final Pattern pattern = Pattern.compile(regex);
		return matchesRegex(pattern);
	}
613247
public static String replaceAll(CharSequence str, String regex, Func1<Matcher, String> replaceFun) {
		return replaceAll(str, Pattern.compile(regex), replaceFun);
	}