List of usage examples for java.util.regex Matcher end
public int end()
From source file:Main.java
public static List<int[]> getLinksRanges(String s) { List<int[]> ranges = new ArrayList<int[]>(); Matcher matcher = WEB_URL_PATTERN.matcher(s); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); ranges.add(new int[] { start, end }); }//from w w w . j a va 2 s. com return ranges; }
From source file:Main.java
public static void findPattern(String regex, String source) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(source); System.out.println("Regex:" + regex); System.out.println("Text:" + source); while (m.find()) { System.out.println("Matched Text:" + m.group() + ", Start:" + m.start() + ", " + "End:" + m.end()); }/*from w w w .java2 s .co m*/ }
From source file:Main.java
private static int[] getIndex(String text, String regex) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); int[] data = new int[2]; // Check all occurrences while (matcher.find()) { data[0] = matcher.start();/*from www . jav a 2s .c o m*/ data[1] = matcher.end(); // System.out.print("Start index: " + matcher.start()); // System.out.print(" End index: " + matcher.end()); // System.out.println(" Found: " + matcher.group()); } return data; }
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 . j a v a 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
private static void match(String REGEX, String INPUT) { Pattern p = Pattern.compile(REGEX); Matcher m = p.matcher(INPUT); int count = 0; while (m.find()) { count++;/*from ww w . j a v a 2 s . co m*/ System.out.println("\nMatch number: " + count); System.out.println("start() : " + m.start()); System.out.println("end() : " + m.end()); System.out.println("group() : " + m.group()); } }
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;//w ww. j av a 2 s . com 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 .ja v a 2s . c om 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;//from w w w. j av a 2s . com 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 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 ww. ja 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; }
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 ww . jav a 2 s .c o m return ss; }