/** * Given a matcher that matches a string against a pattern, * this method returns true when the string matches the pattern or if a longer string, could match the pattern. * * For example: * <pre class="groovyTestCase"> * def emailPattern = /\w+@\w+\.\w{2,}/ * * def matcher = "john@doe" =~ emailPattern * assert matcher.matchesPartially() * * matcher = "john@doe.com" =~ emailPattern * assert matcher.matchesPartially() * * matcher = "john@@" =~ emailPattern * assert !matcher.matchesPartially() * </pre> * * @param matcher the Matcher * @return true if more input to the String could make the matcher match the associated pattern, false otherwise. * * @since 2.0.0 */ public static boolean matchesPartially(Matcher matcher) { return matcher.matches() || matcher.hitEnd(); }
public boolean matches(String text) { Pattern p = pattern(); Matcher m = p.matcher(text); return m.matches() || m.hitEnd(); }
import java.util.regex.*; public class Test { public static void main(String[] args) throws Exception { String[] ss = { "aabb", "aa", "cc", "aac" }; Pattern p = Pattern.compile("aabb"); Matcher m = p.matcher(""); for (String s : ss) { m.reset(s); if (m.matches()) { System.out.printf("%-4s : match%n", s); } else if (m.hitEnd()) { System.out.printf("%-4s : partial match%n", s); } else { System.out.printf("%-4s : no match%n", s); } } } }
public static String camelCaseToConstantCase(String value) { if (value == null || "".equals(value)) return value; StringBuilder result = new StringBuilder(); Matcher matcher = LOWER_UPPER.matcher(value); while (matcher.find()) { String lowers = matcher.group(1); String uppers = matcher.group(2); if (uppers.length() == 0) { result.append(lowers.toUpperCase()); } else { if (lowers.length() > 0) { result.append(lowers.toUpperCase()); result.append('_'); } if (uppers.length() > 1 && !matcher.hitEnd()) { result.append(uppers.substring(0, uppers.length() - 1)); result.append('_'); result.append(uppers.charAt(uppers.length() - 1)); } else { result.append(uppers); } } } return result.toString(); }
@FastMethod @Signature public Memory hitEnd(Environment env, Memory... args) { return matcher.hitEnd() ? Memory.TRUE : Memory.FALSE; }
if ((horizon == 0 && !matcher.hitEnd()) || isHorizonInBuffer || inputExhausted) { result = matcher.group(); break;
String[] inputs = { "AA", "BB" }; Pattern p = Pattern.compile("AAAAAB"); Matcher m = p.matcher(""); for (String s : inputs) { m.reset(s); System.out.printf("%s -- full match: %B; partial match: %B%n", s, m.matches(), m.hitEnd()); }
/** * Returns true if the end of input was hit by the search engine in the * last match operation performed by this matcher. * * @return true iff the end of input was hit in the last match; false otherwise */ public boolean hitEnd() { return matcher.hitEnd(); }
/** * Returns true if the end of input was hit by the search engine in the * last match operation performed by this matcher. * * @return true iff the end of input was hit in the last match; false otherwise */ public boolean hitEnd() { return matcher.hitEnd(); }
Pattern thePattern = Pattern.compile(theRegexString); Matcher m = thePattern.matcher(theStringToTest); if (m.matches()) { return true; } return m.hitEnd();
int farthestPoint(Pattern pattern, String input) { for (int i = input.length - 1; i > 0; i--) { Matcher matcher = pattern.matcher(input.substring(0, i)); if (!matcher.matches() && matcher.hitEnd()) { return i; } } return 0; }
String input="AA"; Pattern pat=Pattern.compile("AAB"); Matcher matcher=pat.matcher(input); System.out.println(matcher.matches()); // prints "false" System.out.println(matcher.hitEnd()); // prints "true"
if (textToParse != null) { Matcher matcher = PLACEHOLDER_PATTERN.matcher(textToParse); while(matcher.hitEnd()!=true){ Boolean result = matcher.find(); int count = matcher.groupCount(); System.out.println("Result " +result+" count "+count); if(result==true && count==1){ mergeFieldName = matcher.group(1); mergeFieldNames.add(mergeFieldName); } } }
Pattern p = Pattern.compile("^2(2[4-9]|3\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); Matcher m = p.matcher("224.."); if (!m.matches() && !m.hitEnd()) { System.out.println("Invalid"); } else { System.out.println("Valid"); }
private final static Pattern WB_PATTERN = Pattern.compile("(?<=\\w)\\b"); private String truncateAfterWords(int n, String s) { if (s == null) return null; if (n <= 0) return ""; Matcher m = WB_PATTERN.matcher(s); for (int i=0; i<n && m.find(); i++); if (m.hitEnd()) return s; else return s.substring(0, m.end()); }
String lString = "154540K->12341K(524288K)"; Pattern lPattern = Pattern.compile("\\d+"); Matcher lMatcher = lPattern.matcher(lString); int lPosition = 2; for (int i = 0; i < lPosition && lMatcher.find(); i++) {} if (!lMatcher.hitEnd()) { System.out.println(lMatcher.group()); }
String english = "Queen. of reptiles."; Pattern p = Pattern.compile("\\w+|\\."); Matcher m = p.matcher(english); do { if (m.find()) { System.out.println(m.group()); } } while (!m.hitEnd());
Pattern p = Pattern.compile("\\G[^,]*,|\\G[^,]+$"); String myCSV = "a,,b"; Matcher m = p.matcher(myCSV); while (m.find()) { System.out.println("Match found from: " + m.start() + " (included) to: " + m.end() + " (excluded)," + " matching: '" + m.group() + "'. Does it hit end?" + m.hitEnd()); }
Matcher base = Pattern.compile("\\bred\\b|\\bgreen\\b|\\bblue\\b|[+()]{1}").matcher(input.trim()); while (!base.hitEnd()) { if (base.find()) { String s = base.group(); System.out.println("Found: " + s); output += String.format(" %s", s); } } if (output.isEmpty()) { throw new IllegalArgumentException("Invalid input no matching tokens found! " + base.toString()); }