Search in sources :

Example 46 with Matcher

use of java.util.regex.Matcher in project druid by druid-io.

the class Granularity method getDateValues.

// Used by the toDate implementations.
final Integer[] getDateValues(String filePath, Formatter formatter) {
    Pattern pattern = defaultPathPattern;
    switch(formatter) {
        case DEFAULT:
        case LOWER_DEFAULT:
            break;
        case HIVE:
            pattern = hivePathPattern;
            break;
        default:
            throw new IAE("Format %s not supported", formatter);
    }
    Matcher matcher = pattern.matcher(filePath);
    // The size is "7" b/c this array contains standard
    // datetime field values namely:
    // year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute,
    // and index 0 is unused.
    Integer[] vals = new Integer[7];
    if (matcher.matches()) {
        for (int i = 1; i <= matcher.groupCount(); i++) {
            vals[i] = (matcher.group(i) != null) ? Integer.parseInt(matcher.group(i)) : null;
        }
    }
    return vals;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) IAE(io.druid.java.util.common.IAE)

Example 47 with Matcher

use of java.util.regex.Matcher in project druid by druid-io.

the class RegexParser method parse.

@Override
public Map<String, Object> parse(String input) {
    try {
        final Matcher matcher = compiled.matcher(input);
        if (!matcher.matches()) {
            throw new ParseException("Incorrect Regex: %s . No match found.", pattern);
        }
        List<String> values = Lists.newArrayList();
        for (int i = 1; i <= matcher.groupCount(); i++) {
            values.add(matcher.group(i));
        }
        if (fieldNames == null) {
            setFieldNames(ParserUtils.generateFieldNames(values.size()));
        }
        return Utils.zipMapPartial(fieldNames, Iterables.transform(values, valueFunction));
    } catch (Exception e) {
        throw new ParseException(e, "Unable to parse row [%s]", input);
    }
}
Also used : Matcher(java.util.regex.Matcher)

Example 48 with Matcher

use of java.util.regex.Matcher in project elasticsearch by elastic.

the class Augmentation method replaceFirst.

/**
     * Replace the first match. Similar to {@link Matcher#replaceFirst(String)} but allows you to customize the replacement based on the
     * match.
     */
public static String replaceFirst(CharSequence receiver, Pattern pattern, Function<Matcher, String> replacementBuilder) {
    Matcher m = pattern.matcher(receiver);
    if (false == m.find()) {
        // CharSequqence's toString is *supposed* to always return the characters in the sequence as a String
        return receiver.toString();
    }
    StringBuffer result = new StringBuffer(initialBufferForReplaceWith(receiver));
    m.appendReplacement(result, Matcher.quoteReplacement(replacementBuilder.apply(m)));
    m.appendTail(result);
    return result.toString();
}
Also used : Matcher(java.util.regex.Matcher)

Example 49 with Matcher

use of java.util.regex.Matcher in project elasticsearch by elastic.

the class Augmentation method replaceAll.

// CharSequence augmentation
/**
     * Replace all matches. Similar to {@link Matcher#replaceAll(String)} but allows you to customize the replacement based on the match.
     */
public static String replaceAll(CharSequence receiver, Pattern pattern, Function<Matcher, String> replacementBuilder) {
    Matcher m = pattern.matcher(receiver);
    if (false == m.find()) {
        // CharSequqence's toString is *supposed* to always return the characters in the sequence as a String
        return receiver.toString();
    }
    StringBuffer result = new StringBuffer(initialBufferForReplaceWith(receiver));
    do {
        m.appendReplacement(result, Matcher.quoteReplacement(replacementBuilder.apply(m)));
    } while (m.find());
    m.appendTail(result);
    return result.toString();
}
Also used : Matcher(java.util.regex.Matcher)

Example 50 with Matcher

use of java.util.regex.Matcher in project elasticsearch by elastic.

the class RestReindexAction method buildRemoteInfo.

static RemoteInfo buildRemoteInfo(Map<String, Object> source) throws IOException {
    @SuppressWarnings("unchecked") Map<String, Object> remote = (Map<String, Object>) source.remove("remote");
    if (remote == null) {
        return null;
    }
    String username = extractString(remote, "username");
    String password = extractString(remote, "password");
    String hostInRequest = requireNonNull(extractString(remote, "host"), "[host] must be specified to reindex from a remote cluster");
    Matcher hostMatcher = HOST_PATTERN.matcher(hostInRequest);
    if (false == hostMatcher.matches()) {
        throw new IllegalArgumentException("[host] must be of the form [scheme]://[host]:[port] but was [" + hostInRequest + "]");
    }
    String scheme = hostMatcher.group("scheme");
    String host = hostMatcher.group("host");
    int port = Integer.parseInt(hostMatcher.group("port"));
    Map<String, String> headers = extractStringStringMap(remote, "headers");
    TimeValue socketTimeout = extractTimeValue(remote, "socket_timeout", RemoteInfo.DEFAULT_SOCKET_TIMEOUT);
    TimeValue connectTimeout = extractTimeValue(remote, "connect_timeout", RemoteInfo.DEFAULT_CONNECT_TIMEOUT);
    if (false == remote.isEmpty()) {
        throw new IllegalArgumentException("Unsupported fields in [remote]: [" + Strings.collectionToCommaDelimitedString(remote.keySet()) + "]");
    }
    return new RemoteInfo(scheme, host, port, queryForRemote(source), username, password, headers, socketTimeout, connectTimeout);
}
Also used : Matcher(java.util.regex.Matcher) RemoteInfo(org.elasticsearch.index.reindex.remote.RemoteInfo) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) TimeValue(org.elasticsearch.common.unit.TimeValue) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue)

Aggregations

Matcher (java.util.regex.Matcher)12473 Pattern (java.util.regex.Pattern)5010 ArrayList (java.util.ArrayList)1516 IOException (java.io.IOException)904 HashMap (java.util.HashMap)565 File (java.io.File)487 Test (org.junit.Test)442 BufferedReader (java.io.BufferedReader)428 Map (java.util.Map)363 List (java.util.List)287 InputStreamReader (java.io.InputStreamReader)266 HashSet (java.util.HashSet)236 MalformedURLException (java.net.MalformedURLException)163 URL (java.net.URL)155 Date (java.util.Date)152 InputStream (java.io.InputStream)147 Field (java.lang.reflect.Field)130 PatternSyntaxException (java.util.regex.PatternSyntaxException)128 ParseException (java.text.ParseException)127 LinkedHashMap (java.util.LinkedHashMap)120