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;
}
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);
}
}
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();
}
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();
}
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);
}
Aggregations