/** * Find the number of Strings matched to the given Matcher. * * @param matcher a Matcher * @return int the number of Strings matched to the given matcher. * @since 1.0 */ public static int getCount(Matcher matcher) { int counter = 0; matcher.reset(); while (matcher.find()) { counter++; } return counter; }
/** * Extracts username and password from the given <code>url</code>. A valid * url to extract {@link Credentials} from looks like: * * <pre> * http://username:password@www.domain.org * </pre> * * @param url the URL to extract {@link Credentials} from * * @return the exracted Credentials or <code>null</code> if the given * <code>url</code> does not contain credentials */ protected static Credentials extractCredentials(String url) { Matcher matcher = URL_CREDENTIALS_PATTERN.matcher(url); if (matcher.matches()) { matcher.reset(); String username = ""; String password = ""; while (matcher.find()) { username = matcher.group(1); password = matcher.group(2); } Credentials credentials = new UsernamePasswordCredentials(username, password); return credentials; } return null; }
private void checkFileNames(Collection<String> files) { Matcher m = IndexFileNames.CODEC_FILE_PATTERN.matcher(""); for (String file : files) { m.reset(file); if (!m.matches()) { throw new IllegalArgumentException("invalid codec filename '" + file + "', must match: " + IndexFileNames.CODEC_FILE_PATTERN.pattern()); } if (file.toLowerCase(Locale.ROOT).endsWith(".tmp")) { throw new IllegalArgumentException("invalid codec filename '" + file + "', cannot end with .tmp extension"); } } }
String nameFor(Object... dataValues) { StringBuffer result = new StringBuffer(); expressionMatcher.reset(); while (expressionMatcher.find()) { String expr = expressionMatcher.group(1); String value = evaluateExpression(expr, dataValues); expressionMatcher.appendReplacement(result, Matcher.quoteReplacement(value)); } expressionMatcher.appendTail(result); iterationCount++; return result.toString(); }
public int parseInto(DateTimeParserBucket bucket, String text, int position) { String substr = text.substring(position); Matcher matcher = numericMatcher.get(); matcher.reset(substr); if (!matcher.matches()) { return -1; } // Joda DateTime only has precision to millis, cut off any fractional portion long millis = Long.parseLong(matcher.group(1)); DateTime dt = new DateTime(millis, ISOChronology.getInstanceUTC()); for (DateTimeFieldType field : dateTimeFields) { bucket.saveField(field, dt.get(field)); } return substr.length(); } }
public LinkedHashMap<String, String> getKeyPropertyList(ObjectName mbeanName) { LinkedHashMap<String, String> keyProperties = keyPropertiesPerBean.get(mbeanName); if (keyProperties == null) { keyProperties = new LinkedHashMap<String, String>(); String properties = mbeanName.getKeyPropertyListString(); Matcher match = PROPERTY_PATTERN.matcher(properties); while (match.lookingAt()) { keyProperties.put(match.group(1), match.group(2)); properties = properties.substring(match.end()); if (properties.startsWith(",")) { properties = properties.substring(1); } match.reset(properties); } keyPropertiesPerBean.put(mbeanName, keyProperties); } return keyProperties; }
boolean[] matchedWord = {false, false}; Matcher[] matchers = {null, null}; matchers[0] = contextWords.words[0].matcher(""); matchers[1] = contextWords.words[1].matcher(""); for (i = 1; i < tokens.length && !matchedWord[0]; i++) { token1 = tokens[i].getToken(); matchedWord[0] = matchers[0].reset(token1).find(); for (j = 1; j < tokens.length && !matchedWord[1]; j++) { token2 = tokens[j].getToken(); matchedWord[1] = matchers[1].reset(token2).find(); String lemma = tokens[i].getAnalyzedToken(j).getLemma(); if (lemma != null && !lemma.isEmpty()) { matchedContext[foundWord] = matchers[foundWord].reset(lemma).find(); matchedContext[foundWord] = matchers[foundWord].reset(token).find(); String lemma = tokens[i].getAnalyzedToken(j).getLemma(); if (lemma != null && !lemma.isEmpty()) { matchedContext[notFoundWord] = matchers[notFoundWord].reset(lemma).find(); matchedContext[notFoundWord] = matchers[notFoundWord].reset(token).find();
private static Iterator<String> splitIntoDocs(Reader r) { if (TREAT_FILE_AS_ONE_DOCUMENT) { return Collections.singleton(IOUtils.slurpReader(r)).iterator(); } else { Collection<String> docs = new ArrayList<>(); ObjectBank<String> ob = ObjectBank.getLineIterator(r); StringBuilder current = new StringBuilder(); Matcher matcher = docPattern.matcher(""); for (String line : ob) { if (matcher.reset(line).lookingAt()) { // Start new doc, store old one if non-empty if (current.length() > 0) { docs.add(current.toString()); current.setLength(0); } } current.append(line).append('\n'); } if (current.length() > 0) { docs.add(current.toString()); } return docs.iterator(); } }
@Override public void processStream(InputStream input) throws IOException { try (BufferedReader in = new BufferedReader(new InputStreamReader(input))) { String line = ""; int lineno = 0; Matcher matcher = BLAME_PATTERN.matcher(line); while ((line = in.readLine()) != null) { ++lineno; matcher.reset(line); if (matcher.find()) { String rev = matcher.group(1); String author = matcher.group(2).trim(); annotation.addLine(rev, author, true); } else { LOGGER.log(Level.SEVERE, "Error: did not find annotation in line {0}: [{1}]", new Object[]{String.valueOf(lineno), line}); } } } } }
/** * Returns true if any of the options in {@code options} matches both of the regular expressions * provided: its name matches the option's name and its value matches the option's value. */ public boolean optionMatches(String namePattern, String valuePattern) { Matcher nameMatcher = Pattern.compile(namePattern).matcher(""); Matcher valueMatcher = Pattern.compile(valuePattern).matcher(""); for (Map.Entry<ProtoMember, Object> entry : map.entrySet()) { if (nameMatcher.reset(entry.getKey().member()).matches() && valueMatcher.reset(String.valueOf(entry.getValue())).matches()) { return true; } } return false; }
@Override public Iterable<String> convertRecord(Class<String> outputSchema, String inputRecord, WorkUnitState workUnit) throws DataConversionException { if (!this.matcher.isPresent()) { this.matcher = Optional.of(this.pattern.matcher(inputRecord)); } else { this.matcher.get().reset(inputRecord); } return this.matcher.get().matches() ? new SingleRecordIterable<>(inputRecord) : new EmptyIterable<String>(); } }
yyyymmdd_matcher.reset(string); int len=string.length(); if(len==8&&yyyymmdd_matcher.find()){ return yyyymmdd_matcher.group(1)+"-"+yyyymmdd_matcher.group(2)+"-"+yyyymmdd_matcher.group(3)+"T00:00:00Z"; yyyy_mm_dd_matcher.reset(string); if(len==10&&yyyy_mm_dd_matcher.find()){ return yyyy_mm_dd_matcher.group(1)+"-"+yyyy_mm_dd_matcher.group(2)+"-"+yyyy_mm_dd_matcher.group(3)+"T00:00:00Z"; yyyy_mm_dd_2_matcher.reset(string); if(len==10&&yyyy_mm_dd_2_matcher.find()){ return yyyy_mm_dd_2_matcher.group(1)+"-"+yyyy_mm_dd_2_matcher.group(2)+"-"+yyyy_mm_dd_2_matcher.group(3)+"T00:00:00Z"; yyyymmddhhsshh_matcher.reset(string); if(len==14&&yyyymmddhhsshh_matcher.find()){ yyyy_mm_dd_hh_ss_hh_matcher.reset(string); if(len==19&&yyyy_mm_dd_hh_ss_hh_matcher.find()){ valid_matcher.reset(string); if(valid_matcher.find()){
patternMatcher.reset(strVal); if (patternMatcher.matches()) { String field = patternMatcher.group(1); if (field != null && field.equals("-")) { sign = -1; DateUtils.parseNumericValueWithRange("year", patternMatcher.group(2), 0, Integer.MAX_VALUE); byte months = (byte) (sign * DateUtils.parseNumericValueWithRange("month", patternMatcher.group(3), 0, 11)); result = new HiveIntervalYearMonth(years, months); } catch (Exception err) {
/** * Add a key-value pair to pass to Tesseract using its -c command line option. * To see the possible options, run tesseract --print-parameters. * * You may also add these parameters in TesseractOCRConfig.properties; any * key-value pair in the properties file where the key contains an underscore * is passed directly to Tesseract. * * @param key * @param value */ public void addOtherTesseractConfig(String key, String value) { if (key == null) { throw new IllegalArgumentException("key must not be null"); } if (value == null) { throw new IllegalArgumentException("value must not be null"); } Matcher m = ALLOWABLE_OTHER_PARAMS_PATTERN.matcher(key); if (! m.find()) { throw new IllegalArgumentException("Key contains illegal characters: "+key); } m.reset(value); if (! m.find()) { throw new IllegalArgumentException("Value contains illegal characters: "+value); } otherTesseractConfig.put(key.trim(), value.trim()); }
public static void checkLinesAgainstRegexp(String resultPath, String regexp){ Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(""); ArrayList<String> list = new ArrayList<>(); try { readAllResultLines(list, resultPath, new String[]{}, false); } catch (IOException e1) { Assert.fail("Error reading the result"); } for (String line : list){ matcher.reset(line); if (!matcher.find()){ String msg = "Line is not well-formed: " + line; Assert.fail(msg); } } }