List of usage examples for java.util.regex Matcher start
public int start()
From source file:Main.java
public static SpannableString StringToBitMap(Context context, String msg) { SpannableString ss = new SpannableString(msg); Pattern p = Pattern.compile("/mnt/sdcard/.+?\\.\\w{3}"); Matcher m = p.matcher(msg); while (m.find()) { Bitmap bitmap = BitmapFactory.decodeFile(m.group()); ImageSpan imageSpan = new ImageSpan(context, bitmap); ss.setSpan(imageSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }/*from w w w. ja va2s. c o m*/ return ss; }
From source file:Main.java
/** * replace existing spannable with smiles * /*from w w w . ja v a 2 s . c om*/ * @param context * @param spannable * @return */ public static boolean addSmiles(Context context, Spannable spannable) { boolean hasChanges = false; for (Entry<Pattern, Integer> entry : emoticons.entrySet()) { Matcher matcher = entry.getKey().matcher(spannable); while (matcher.find()) { boolean set = true; for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end()) spannable.removeSpan(span); else { set = false; break; } if (set) { hasChanges = true; spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return hasChanges; }
From source file:com.github.ibole.infrastructure.persistence.db.mybatis.pagination.SqlHelper.java
private static int indexOfByRegex(String input, String regex) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); if (m.find()) { return m.start(); }// w w w . j a va2 s. co m return -1; }
From source file:Main.java
/** * Escape some special character as HTML escape sequence. * /*from w ww . j av a 2 s. co m*/ * @param text Text to be displayed using WebView. * @return Text correctly escaped. */ public static String escapeCharacterToDisplay(String text) { Pattern pattern = PLAIN_TEXT_TO_ESCAPE; Matcher match = pattern.matcher(text); if (match.find()) { StringBuilder out = new StringBuilder(); int end = 0; do { int start = match.start(); out.append(text.substring(end, start)); end = match.end(); int c = text.codePointAt(start); if (c == ' ') { // Escape successive spaces into series of " ". for (int i = 1, n = end - start; i < n; ++i) { out.append(" "); } out.append(' '); } else if (c == '\r' || c == '\n') { out.append("<br>"); } else if (c == '<') { out.append("<"); } else if (c == '>') { out.append(">"); } else if (c == '&') { out.append("&"); } } while (match.find()); out.append(text.substring(end)); text = out.toString(); } return text; }
From source file:Main.java
public static String escapeXPathField(String field) { Matcher matcher = Pattern.compile("['\"]").matcher(field); StringBuilder buffer = new StringBuilder("concat("); int start = 0; while (matcher.find()) { buffer.append("'").append(field.substring(start, matcher.start())).append("',"); buffer.append("'".equals(matcher.group()) ? "\"'\"," : "'\"',"); start = matcher.end();//from w w w.j a va 2s. c o m } if (start == 0) return "'" + field + "'"; return buffer.append("'").append(field.substring(start)).append("'").append(")").toString(); }
From source file:Main.java
/** * replace existing spannable with smiles * @param context//from w w w . j av a 2 s . c o m * @param spannable * @return */ public static boolean addSmiles(Context context, Spannable spannable) { boolean hasChanges = false; for (Entry<Pattern, Object> entry : emoticons.entrySet()) { Matcher matcher = entry.getKey().matcher(spannable); while (matcher.find()) { boolean set = true; for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end()) spannable.removeSpan(span); else { set = false; break; } if (set) { hasChanges = true; Object value = entry.getValue(); if (value instanceof String && !((String) value).startsWith("http")) { File file = new File((String) value); if (!file.exists() || file.isDirectory()) { return false; } spannable.setSpan(new ImageSpan(context, Uri.fromFile(file)), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else { spannable.setSpan(new ImageSpan(context, (Integer) value), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } } return hasChanges; }
From source file:Main.java
public static String replaceCheatSheetEmojis(String s) { if (TextUtils.isEmpty(s)) { return ""; }//from w w w. j a v a 2s . c o m Matcher matcher = COLON_REGEX.matcher(s); List<int[]> linkRanges = getLinksRanges(s); List<int[]> potentialMatches = new ArrayList<int[]>(); OUTER: while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); for (int[] range : linkRanges) { if (start <= range[1] && range[0] <= end) { continue OUTER; } } potentialMatches.add(0, new int[] { start, end }); } for (int[] potentialMatch : potentialMatches) { String toReplace = s.substring(potentialMatch[0], potentialMatch[1]); String replacement = CHEAT_SHEET_TO_UNICODE.get(toReplace); if (!TextUtils.isEmpty(replacement)) { String newString = s.substring(0, potentialMatch[0]) + replacement; if (potentialMatch[1] <= s.length()) { newString += s.substring(potentialMatch[1]); } s = newString; } } return s; }
From source file:msi.gama.common.util.StringUtils.java
public static List<String> tokenize(final String expression) { if (expression == null) { return Collections.EMPTY_LIST; }//from www . ja va 2 s. co m final Pattern p = Pattern.compile(regex); final List<String> tokens = new ArrayList<String>(); final Matcher m = p.matcher(expression); while (m.find()) { tokens.add(expression.substring(m.start(), m.end())); } return tokens; }
From source file:de.bayern.gdi.utils.StringUtils.java
/** * Splits a string by a pattern.// w ww.j av a2s.c om * @param s The string to split. * @param delim The delimeter. * @param keep Indicates if the delimeters should be kept. * @return The splitted string. */ public static String[] split(String s, Pattern delim, boolean keep) { if (s == null) { s = ""; } int lastMatch = 0; ArrayList<String> parts = new ArrayList<>(); Matcher m = delim.matcher(s); while (m.find()) { String x = s.substring(lastMatch, m.start()); if (!x.isEmpty()) { parts.add(x); } if (keep) { parts.add(m.group(0)); } lastMatch = m.end(); } String x = s.substring(lastMatch); if (!x.isEmpty()) { parts.add(x); } return parts.toArray(new String[parts.size()]); }
From source file:cf.adriantodt.utils.HTML2Discord.java
public static String toPlainText(String discordFormatMessage) { String strippedContent;//from ww w . j a v a 2 s . c om //all the formatting keys to keep track of String[] keys = new String[] { "*", "_", "`", "~~" }; //find all tokens (formatting strings described above) TreeSet<FormatToken> tokens = new TreeSet<>((t1, t2) -> Integer.compare(t1.start, t2.start)); for (String key : keys) { Matcher matcher = Pattern.compile(Pattern.quote(key)).matcher(discordFormatMessage); while (matcher.find()) { tokens.add(new FormatToken(key, matcher.start())); } } //iterate over all tokens, find all matching pairs, and add them to the list toRemove Stack<FormatToken> stack = new Stack<>(); List<FormatToken> toRemove = new ArrayList<>(); boolean inBlock = false; for (FormatToken token : tokens) { if (stack.empty() || !stack.peek().format.equals(token.format) || stack.peek().start + token.format.length() == token.start) { //we are at opening tag if (!inBlock) { //we are outside of block -> handle normally if (token.format.equals("`")) { //block start... invalidate all previous tags stack.clear(); inBlock = true; } stack.push(token); } else if (token.format.equals("`")) { //we are inside of a block -> handle only block tag stack.push(token); } } else if (!stack.empty()) { //we found a matching close-tag toRemove.add(stack.pop()); toRemove.add(token); if (token.format.equals("`") && stack.empty()) { //close tag closed the block inBlock = false; } } } //sort tags to remove by their start-index and iteratively build the remaining string Collections.sort(toRemove, (t1, t2) -> Integer.compare(t1.start, t2.start)); StringBuilder out = new StringBuilder(); int currIndex = 0; for (FormatToken formatToken : toRemove) { if (currIndex < formatToken.start) { out.append(discordFormatMessage.substring(currIndex, formatToken.start)); } currIndex = formatToken.start + formatToken.format.length(); } if (currIndex < discordFormatMessage.length()) { out.append(discordFormatMessage.substring(currIndex)); } //return the stripped text, escape all remaining formatting characters (did not have matching open/close before or were left/right of block strippedContent = out.toString().replace("*", "\\*").replace("_", "\\_").replace("~", "\\~"); return strippedContent; }