List of usage examples for java.util.regex Matcher start
public int start()
From source file:Main.java
/** * Converts a given message to CTCP formatting. * * @param message message to convert//w w w .j a va2s . c om * @return converted message */ static String toCTCP(String message) { StringBuilder builder = new StringBuilder(); builder.append(CTCP_DELIMITER); int currentIndex = 0; Matcher matcher = CTCP_ESCAPABLE_CHAR.matcher(message); while (matcher.find()) { if (matcher.start() > currentIndex) { builder.append(message.substring(currentIndex, matcher.start())); } switch (matcher.group()) { case "\n": builder.append(CTCP_MQUOTE).append('n'); break; case "\r": builder.append(CTCP_MQUOTE).append('r'); break; case "\u0000": builder.append(CTCP_MQUOTE).append('0'); break; case CTCP_MQUOTE + "": builder.append(CTCP_MQUOTE).append(CTCP_MQUOTE); break; case CTCP_DELIMITER + "": builder.append("\\a"); break; case "\\": builder.append("\\\\"); break; } currentIndex = matcher.end(); } if (currentIndex < message.length()) { builder.append(message.substring(currentIndex)); } builder.append(CTCP_DELIMITER); return builder.toString(); }
From source file:Correct.java
public static void execute(String One, String Two) { Two = Two.replaceAll("\\\\n", "\n"); try {//from w w w . j av a 2 s .c om System.out.println("Regex = " + One); System.out.println("Input = " + Two); Pattern pattern = Pattern.compile(One); Matcher match = pattern.matcher(Two); while (match.find()) { System.out.println("Found [" + match.group() + "]\nStarting at " + match.start() + " , \nEnding at " + (match.end() - 1)); } } catch (PatternSyntaxException pse) { System.err.println("Bad regex: " + pse.getMessage()); System.err.println("Description: " + pse.getDescription()); System.err.println("Index: " + pse.getIndex()); System.err.println("Incorrect pattern: " + pse.getPattern()); } }
From source file:Main.java
/** * Converts a method name to an attribute name, e.g. getFooBar() --> foo_bar, isFlag --> flag. * @param methodName// w w w .ja va 2s .co m * @return */ public static String methodNameToAttributeName(final String methodName) { String name = methodName; if ((methodName.startsWith("get") || methodName.startsWith("set")) && methodName.length() > 3) name = methodName.substring(3); else if (methodName.startsWith("is") && methodName.length() > 2) name = methodName.substring(2); // Pattern p=Pattern.compile("[A-Z]+"); Matcher m = METHOD_NAME_TO_ATTR_NAME_PATTERN.matcher(name); StringBuffer sb = new StringBuffer(); while (m.find()) { int start = m.start(), end = m.end(); String str = name.substring(start, end).toLowerCase(); if (str.length() > 1) { String tmp1 = str.substring(0, str.length() - 1); String tmp2 = str.substring(str.length() - 1); str = tmp1 + "_" + tmp2; } if (start == 0) { m.appendReplacement(sb, str); } else m.appendReplacement(sb, "_" + str); } m.appendTail(sb); return sb.length() > 0 ? sb.toString() : methodName; }
From source file:Main.java
/** * Converts a given message from CTCP escaping. * * @param message message to convert// ww w .j a v a 2 s . c om * @return converted message */ static String fromCTCP(String message) { message = message.substring(1); // Strip the starting delimiter message = message.substring(0, message.indexOf(CTCP_DELIMITER)); // Strip the second delimiter StringBuilder builder = new StringBuilder(); int currentIndex = 0; Matcher matcher = CTCP_ESCAPED_CHAR.matcher(message); while (matcher.find()) { if (matcher.start() > currentIndex) { builder.append(message.substring(currentIndex, matcher.start())); } switch (matcher.group(1)) { case CTCP_MQUOTE + "": switch (matcher.group(2)) { case "n": builder.append('\n'); break; case "r": builder.append('\r'); break; case "0": builder.append('\u0000'); break; default: builder.append(matcher.group(2)); // If not one of the above, disregard the MQUOTE. If MQUOTE, it's covered here anyway. } break; case "\\": switch (matcher.group(2)) { case "a": builder.append(CTCP_DELIMITER); break; default: builder.append(matcher.group(2)); // If not one of the above, disregard the \. If \, it's covered here anyway. } } currentIndex = matcher.end(); } if (currentIndex < message.length()) { builder.append(message.substring(currentIndex)); } return builder.toString(); }
From source file:Main.java
public static String decodeUnicode(String str) { Charset set = Charset.forName("UTF-16"); Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})"); Matcher m = p.matcher(str); int start = 0; int start2;//from w w w.ja va2s . co m StringBuffer sb = new StringBuffer(); while (m.find(start)) { start2 = m.start(); if (start2 > start) { String seg = str.substring(start, start2); sb.append(seg); } String code = m.group(1); int i = Integer.valueOf(code, 16); byte[] bb = new byte[4]; bb[0] = (byte) ((i >> 8) & 0xFF); bb[1] = (byte) (i & 0xFF); ByteBuffer b = ByteBuffer.wrap(bb); sb.append(String.valueOf(set.decode(b)).trim()); start = m.end(); } start2 = str.length(); if (start2 > start) { String seg = str.substring(start, start2); sb.append(seg); } return sb.toString(); }
From source file:Main.java
public static boolean addEmoticons(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;/*from w ww .ja va 2 s . c o m*/ 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:Main.java
public static String processCaptionText(String text) { String result = text;//from w w w .j av a 2 s . c o m Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(text); ArrayList<Integer[]> toDelete = new ArrayList<>(); while (matcher.find()) { Integer[] tmpArray = new Integer[] { matcher.start(), matcher.end() }; toDelete.add(0, tmpArray); } for (Integer[] tmpArray : toDelete) { if (tmpArray[0] > 0 && result.substring(tmpArray[0] - 1, tmpArray[0]).compareTo(" ") == 0) tmpArray[0]--; if (tmpArray[0] == 0 && (tmpArray[1] + 1) < result.length()) tmpArray[1]++; result = result.substring(0, tmpArray[0]) + result.substring(tmpArray[1]); } return result; }
From source file:Main.java
public static boolean addSmiles(Context context, Spannable spannable) { boolean hasChanges = false; for (Map.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;// w ww. jav a 2 s . co m 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:Main.java
public static int getCharacterPosition(String string, int next, String sub) { Matcher slashMatcher = Pattern.compile(sub).matcher(string); int index = 0; while (slashMatcher.find()) { index++;//ww w . j av a2 s. c o m if (index == next) break; } return slashMatcher.start(); }
From source file:Main.java
public static boolean addSmiles(Context context, Spannable spannable) { boolean hasChanges = false; for (Map.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;//from w w w .j a v a 2s . c o m break; } } if (set) { hasChanges = true; spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return hasChanges; }