use of java.util.regex.Pattern in project hackpad by dropbox.
the class CssCompressor method extractDataUrls.
// Leave data urls alone to increase parse performance.
protected String extractDataUrls(String css, ArrayList preservedTokens) {
int maxIndex = css.length() - 1;
int appendIndex = 0;
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("(?i)url\\(\\s*([\"']?)data\\:");
Matcher m = p.matcher(css);
while (m.find()) {
// "url(".length()
int startIndex = m.start() + 4;
// ', " or empty (not quoted)
String terminator = m.group(1);
if (terminator.length() == 0) {
terminator = ")";
}
boolean foundTerminator = false;
int endIndex = m.end() - 1;
while (foundTerminator == false && endIndex + 1 <= maxIndex) {
endIndex = css.indexOf(terminator, endIndex + 1);
if ((endIndex > 0) && (css.charAt(endIndex - 1) != '\\')) {
foundTerminator = true;
if (!")".equals(terminator)) {
endIndex = css.indexOf(")", endIndex);
}
}
}
// Enough searching, start moving stuff over to the buffer
sb.append(css.substring(appendIndex, m.start()));
if (foundTerminator) {
String token = css.substring(startIndex, endIndex);
token = token.replaceAll("\\s+", "");
preservedTokens.add(token);
String preserver = "url(___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___)";
sb.append(preserver);
appendIndex = endIndex + 1;
} else {
// No end terminator found, re-add the whole match. Should we throw/warn here?
sb.append(css.substring(m.start(), m.end()));
appendIndex = m.end();
}
}
sb.append(css.substring(appendIndex));
return sb.toString();
}
use of java.util.regex.Pattern in project deeplearning4j by deeplearning4j.
the class StringUtils method allMatches.
public static List<Matcher> allMatches(String str, String regex) {
Pattern p = Pattern.compile(regex);
List<Matcher> matches = new ArrayList<>();
while (true) {
Matcher m = p.matcher(str);
if (!m.find())
break;
matches.add(m);
str = str.substring(m.end());
}
return matches;
}
use of java.util.regex.Pattern 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.Pattern in project druid by druid-io.
the class S3TimestampVersionedDataFinderTest method testMissing.
@Test
public void testMissing() throws S3ServiceException {
String bucket = "bucket";
String keyPrefix = "prefix/dir/0";
RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
S3Object object0 = new S3Object(), object1 = new S3Object();
object0.setBucketName(bucket);
object0.setKey(keyPrefix + "/renames-0.gz");
object0.setLastModifiedDate(new Date(0));
object1.setBucketName(bucket);
object1.setKey(keyPrefix + "/renames-1.gz");
object1.setLastModifiedDate(new Date(1));
EasyMock.expect(s3Client.listObjects(EasyMock.eq(bucket), EasyMock.anyString(), EasyMock.<String>isNull())).andReturn(null).once();
S3TimestampVersionedDataFinder finder = new S3TimestampVersionedDataFinder(s3Client);
Pattern pattern = Pattern.compile("renames-[0-9]*\\.gz");
EasyMock.replay(s3Client);
URI latest = finder.getLatestVersion(URI.create(String.format("s3://%s/%s", bucket, keyPrefix)), pattern);
EasyMock.verify(s3Client);
Assert.assertEquals(null, latest);
}
use of java.util.regex.Pattern in project druid by druid-io.
the class S3TimestampVersionedDataFinderTest method testFindSelf.
@Test
public void testFindSelf() throws S3ServiceException {
String bucket = "bucket";
String keyPrefix = "prefix/dir/0";
RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
S3Object object0 = new S3Object();
object0.setBucketName(bucket);
object0.setKey(keyPrefix + "/renames-0.gz");
object0.setLastModifiedDate(new Date(0));
EasyMock.expect(s3Client.listObjects(EasyMock.eq(bucket), EasyMock.anyString(), EasyMock.<String>isNull())).andReturn(new S3Object[] { object0 }).once();
S3TimestampVersionedDataFinder finder = new S3TimestampVersionedDataFinder(s3Client);
Pattern pattern = Pattern.compile("renames-[0-9]*\\.gz");
EasyMock.replay(s3Client);
URI latest = finder.getLatestVersion(URI.create(String.format("s3://%s/%s", bucket, keyPrefix)), pattern);
EasyMock.verify(s3Client);
URI expected = URI.create(String.format("s3://%s/%s", bucket, object0.getKey()));
Assert.assertEquals(expected, latest);
}
Aggregations