1 package org.unicode.cldr.util; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class RegexUtilities { 7 findMismatch(Matcher m, CharSequence s)8 public static int findMismatch(Matcher m, CharSequence s) { 9 int i; 10 for (i = 1; i < s.length(); ++i) { 11 boolean matches = m.reset(s.subSequence(0, i)).matches(); 12 if (!matches && !m.hitEnd()) { 13 break; 14 } 15 } 16 return i - 1; 17 } 18 findMismatch(Pattern p, CharSequence s)19 public static int findMismatch(Pattern p, CharSequence s) { 20 Matcher m = p.matcher(""); 21 return findMismatch(m, s); 22 } 23 showMismatch(Matcher m, CharSequence s)24 public static String showMismatch(Matcher m, CharSequence s) { 25 int failPoint = findMismatch(m, s); 26 String show = s.subSequence(0, failPoint) + "☹" + s.subSequence(failPoint, s.length()); 27 return show; 28 } 29 showMismatch(Pattern p, CharSequence s)30 public static String showMismatch(Pattern p, CharSequence s) { 31 Matcher m = p.matcher(""); 32 return showMismatch(m, s); 33 } 34 } 35