Java Code Examples for com.google.re2j.Matcher#find()

The following examples show how to use com.google.re2j.Matcher#find() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterReplace.java    From copybara with Apache License 2.0 6 votes vote down vote up
private String replaceString(String originalContent) {
  Pattern pattern = Pattern.compile(before.pattern());
  Matcher matcher = pattern.matcher(originalContent);

  boolean anyReplace = false;
  StringBuilder result = new StringBuilder(originalContent.length());
  while (matcher.find()) {
    String val = matcher.group(FilterReplace.this.group);
    String res = mapping.apply(val);
    anyReplace |= !val.equals(res);
    if (group == 0) {
      matcher.appendReplacement(result, Matcher.quoteReplacement(res));
    } else {
      String prefix = originalContent.substring(matcher.start(), matcher.start(group));
      String suffix = originalContent.substring(matcher.end(group), matcher.end());
      matcher.appendReplacement(result, Matcher.quoteReplacement((prefix + res + suffix)));
    }
  }

  if (!anyReplace) {
    return originalContent;
  }

  matcher.appendTail(result);
  return result.toString();
}
 
Example 2
Source File: RouteMethod.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateModel exec(List arguments) throws TemplateModelException {
    String url;
    if (arguments.size() >= MIN_ARGUMENTS) {
        String controller = ((SimpleScalar) arguments.get(0)).getAsString();
        RequestRoute requestRoute = Router.getReverseRoute(controller);
        
        if (requestRoute != null) {
            url = requestRoute.getUrl();
            Matcher matcher = PARAMETER_PATTERN.matcher(url);
            int i = 1;
            while (matcher.find()) {
                String argument = ((SimpleScalar) arguments.get(i)).getAsString();
                url = StringUtils.replace(url, "{" + matcher.group(1) + "}", argument);
                i++;
            }
        } else {
            throw new TemplateModelException("Reverse route for " + controller + " could not be found!");
        }
    } else {
        throw new TemplateModelException("Missing at least one argument (ControllerClass:ControllerMethod) for reverse routing!");
    }

    return new SimpleScalar(url);
}
 
Example 3
Source File: RdeUtil.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Look at some bytes from {@code xmlInput} to ensure it appears to be a FULL XML deposit and
 * then use a regular expression to extract the watermark timestamp which is returned.
 */
public static DateTime peekWatermark(BufferedInputStream xmlInput)
    throws IOException, XmlException {
  xmlInput.mark(PEEK_SIZE);
  byte[] peek = new byte[PEEK_SIZE];
  if (xmlInput.read(peek) != PEEK_SIZE) {
    throw new IOException(String.format("Failed to peek %,d bytes on input file", PEEK_SIZE));
  }
  xmlInput.reset();
  String peekStr = new String(peek, UTF_8);
  if (!peekStr.contains("urn:ietf:params:xml:ns:rde-1.0")) {
    throw new XmlException(String.format(
        "Does not appear to be an XML RDE deposit\n%s", dumpHex(peek)));
  }
  if (!peekStr.contains("type=\"FULL\"")) {
    throw new XmlException("Only FULL XML RDE deposits suppported at this time");
  }
  Matcher watermarkMatcher = WATERMARK_PATTERN.matcher(peekStr);
  if (!watermarkMatcher.find()) {
    throw new XmlException("Could not find RDE watermark in XML");
  }
  return DATETIME_FORMATTER.parseDateTime(watermarkMatcher.group(1));
}
 
Example 4
Source File: JavaAnalyzer.java    From coderadar with MIT License 6 votes vote down vote up
/**
 * Get imports from a given line.
 *
 * @param line line to check.
 * @return list of dependency Strings.
 */
public List<String> getDependenciesFromImportLine(String line) {
  // possibilities for there to be more than one dependency in one line
  //   import org.somepackage.A a; import org.somepackage.B;
  List<String> foundDependencies = new ArrayList<>();
  String lineString = line;
  while (lineString.contains(";")) {
    Matcher importMatcher = importPattern.matcher(lineString);
    if (importMatcher.find()) {
      String importString =
          importMatcher.group().substring(1, importMatcher.group().length() - 1);
      if (!foundDependencies.contains(importString)) {
        foundDependencies.add(importString);
      }
    }
    lineString = lineString.substring(lineString.indexOf(";") + 1);
  }
  return foundDependencies;
}
 
Example 5
Source File: SqlTemplate.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the freshly substituted SQL code.
 *
 * @throws IllegalArgumentException if any substitution variable is not found in the template,
 *         or if there are any variable-like strings (%something%) left after substitution.
 */
public String build() {
  StringBuffer result = new StringBuffer(template.length());
  Set<String> found = new HashSet<>();
  Matcher matcher = SEARCH_PATTERN.matcher(template);
  while (matcher.find()) {
    String wholeMatch = matcher.group(0);
    String leftQuote = matcher.group(1);
    String key = matcher.group(2);
    String rightQuote = matcher.group(3);
    String value = substitutions.get(key);
    checkArgumentNotNull(value, "%%s% found in template but no substitution specified", key);
    checkArgument(leftQuote.equals(rightQuote), "Quote mismatch: %s", wholeMatch);
    matcher.appendReplacement(result, String.format("%s%s%s", leftQuote, value, rightQuote));
    found.add(key);
  }
  matcher.appendTail(result);
  Set<String> remaining = difference(substitutions.keySet(), found);
  checkArgument(remaining.isEmpty(),
      "Not found in template: %s", Joiner.on(", ").join(remaining));
  return result.toString();
}
 
Example 6
Source File: JavaAnalyzer.java    From coderadar with MIT License 5 votes vote down vote up
public String getPackageName(byte[] byteFileContent) {
  if (byteFileContent != null) {
    String fileContent = clearFileContent(new String(byteFileContent));
    Matcher packageMatcher = cache.getPattern(PACKAGE_PATTERN).matcher(fileContent);
    if (packageMatcher.find()) {
      Matcher nameMatcher = cache.getPattern(NAME_PATTERN).matcher(packageMatcher.group());
      if (nameMatcher.find()) {
        return nameMatcher.group().substring(1);
      }
    }
  }
  return "";
}
 
Example 7
Source File: MarkdownGenerator.java    From copybara with Apache License 2.0 5 votes vote down vote up
private String simplerJavaTypes(TypeMirror typeMirror) {
  String s = typeMirror.toString();
  Matcher m = Pattern.compile("(?:[A-z.]*\\.)*([A-z]+)").matcher(s);
  StringBuilder sb = new StringBuilder();
  while(m.find()) {
    String replacement = deCapitalize(m.group(1));
    m.appendReplacement(sb, replacement);
  }
  m.appendTail(sb);

  return HtmlEscapers.htmlEscaper().escape(sb.toString());
}
 
Example 8
Source File: TodoReplace.java    From copybara with Apache License 2.0 5 votes vote down vote up
private Set<FileState> run(Iterable<FileState> files, Console console)
    throws IOException, ValidationException {
  Set<FileState> modifiedFiles = new HashSet<>();
  // TODO(malcon): Remove reconstructing pattern once RE2J doesn't synchronize on matching.
  Pattern batchPattern = Pattern.compile(pattern.pattern(), pattern.flags());
  for (FileState file : files) {
    if (Files.isSymbolicLink(file.getPath())) {
      continue;
    }
    String content = new String(Files.readAllBytes(file.getPath()), UTF_8);
    Matcher matcher = batchPattern.matcher(content);
    StringBuffer sb = new StringBuffer();
    boolean modified = false;
    while (matcher.find()) {
      if (matcher.group(2).trim().isEmpty()){
        matcher.appendReplacement(sb, matcher.group(0));
        continue;
      }
      List<String> users = Splitter.on(",").splitToList(matcher.group(2));
      List<String> mappedUsers = mapUsers(users, matcher.group(0), file.getPath(), console);
      modified |= !users.equals(mappedUsers);
      String result = matcher.group(1);
      if (!mappedUsers.isEmpty()) {
        result += "(" + Joiner.on(",").join(mappedUsers) + ")";
      }
      matcher.appendReplacement(sb, Matcher.quoteReplacement(result));
    }
    matcher.appendTail(sb);

    if (modified) {
      modifiedFiles.add(file);
      Files.write(file.getPath(), sb.toString().getBytes(UTF_8));
    }
  }
  return modifiedFiles;
}
 
Example 9
Source File: LabelTemplate.java    From copybara with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the template object from a String
 * @param template a String in the from of "Foo ${LABEL} ${OTHER} Bar"
 */
public LabelTemplate(String template) {
  this.template = template;
  Matcher matcher = VAR_PATTERN.matcher(template);
  while (matcher.find()) {
    labels.add(matcher.group(1));
  }
}
 
Example 10
Source File: MarkdownDocumentationFormatter.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the string with all HTML tags stripped.  Also, removes a single space after any
 * newlines that have one (we get a single space indent for all lines but the first because of
 * the way that javadocs are written in comments).
 */
@VisibleForTesting
static String fixHtml(String value) {
  Matcher matcher = HTML_TAG_AND_ENTITY_PATTERN.matcher(value);
  int pos = 0;
  StringBuilder result = new StringBuilder();
  while (matcher.find(pos)) {
    result.append(value, pos, matcher.start());
    switch (matcher.group(0)) {
      case "<p>":
        // <p> is simply removed.
        break;
      case "&amp;":
        result.append("&");
        break;
      case "&lt;":
        result.append("<");
        break;
      case "&gt;":
        result.append(">");
        break;
      case "&squot;":
        result.append("'");
        break;
      case "&quot;":
        result.append("\"");
        break;
      default:
        throw new IllegalArgumentException("Unrecognized HTML sequence: " + matcher.group(0));
    }
    pos = matcher.end();
  }

  // Add the string after the last HTML sequence.
  result.append(value.substring(pos));

  return result.toString().replace("\n ", "\n");
}
 
Example 11
Source File: Https.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
public static String getCommonName(InetSocketAddress addr) throws Exception {
	SSLSocketFactory ssf = HttpsURLConnection.getDefaultSSLSocketFactory();
    SSLSocket socket = (SSLSocket) ssf.createSocket(addr.getAddress(), addr.getPort());
    socket.startHandshake();
    SSLSession session = socket.getSession();
    X509Certificate[] servercerts = (X509Certificate[]) session.getPeerCertificates();

    Pattern pattern = Pattern.compile("CN=([^,]+)", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(servercerts[0].getSubjectDN().getName());
    if (matcher.find()) {
    	return matcher.group(1);
    }
    return "";
}
 
Example 12
Source File: Http.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
private void analyzeResponseStatusLine(String status_line) throws Exception
{
	Pattern pattern = Pattern.compile("[^ ]+ +([^ ]+) +([a-z0-9A-Z ]+)$");
	Matcher matcher = pattern.matcher(status_line);
	if (matcher.find()) {
		this.statusCode = matcher.group(1).trim();
		//			if (matcher.group(2).trim().equals("Connection established")) {
		//				//flag_httpsは使ってない
		//				flag_https = true;
		//			}
	}
}
 
Example 13
Source File: Http.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private String replaceStatusLineToNonProxyStyte(String status_line) throws Exception
{
	String result = status_line;
	Pattern pattern = Pattern.compile("http://[^/]+");
	Matcher matcher = pattern.matcher(status_line);
	if (matcher.find()) {
		result = matcher.replaceAll("");
	}
	return result;
}
 
Example 14
Source File: Modification.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
private byte[] replaceRegex(byte[] data, Packet packet) {
	Pattern pattern = Pattern.compile(this.pattern, Pattern.MULTILINE);
	Matcher matcher = pattern.matcher(new String(data));
	String result = new String(data);
	while (matcher.find()) {
		result = matcher.replaceAll(this.replaced);
		packet.setModified();
	}
	return result.getBytes();
}
 
Example 15
Source File: Re2JRegexp.java    From presto with Apache License 2.0 5 votes vote down vote up
public Block split(Slice source)
{
    Matcher matcher = re2jPattern.matcher(source);
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);

    int lastEnd = 0;
    while (matcher.find()) {
        Slice slice = source.slice(lastEnd, matcher.start() - lastEnd);
        lastEnd = matcher.end();
        VARCHAR.writeSlice(blockBuilder, slice);
    }

    VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
    return blockBuilder.build();
}
 
Example 16
Source File: Re2JRegexpFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@ScalarFunction
@Description("Returns the number of times that a pattern occurs in a string")
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long regexpCount(@SqlType("varchar(x)") Slice source, @SqlType(Re2JRegexpType.NAME) Re2JRegexp pattern)
{
    Matcher matcher = pattern.matcher(source);

    int count = 0;
    while (matcher.find()) {
        count++;
    }

    return count;
}
 
Example 17
Source File: Re2JRegexpFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@ScalarFunction
@Description("Returns the index of the n-th matched substring starting from the specified position")
@LiteralParameters("x")
@SqlType(StandardTypes.INTEGER)
public static long regexpPosition(
        @SqlType("varchar(x)") Slice source,
        @SqlType(Re2JRegexpType.NAME) Re2JRegexp pattern,
        @SqlType(StandardTypes.INTEGER) long start,
        @SqlType(StandardTypes.INTEGER) long occurrence)
{
    // start position cannot be smaller than 1
    if (start < 1) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "start position cannot be smaller than 1");
    }
    // occurrence cannot be smaller than 1
    if (occurrence < 1) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "occurrence cannot be smaller than 1");
    }
    // returns -1 if start is greater than the length of source
    if (start > SliceUtf8.countCodePoints(source)) {
        return -1;
    }

    int startBytePosition = SliceUtf8.offsetOfCodePoint(source, (int) start - 1);
    int length = source.length() - startBytePosition;
    Matcher matcher = pattern.matcher(source.slice(startBytePosition, length));
    long count = 0;
    while (matcher.find()) {
        if (++count == occurrence) {
            // Plus 1 because position returned start from 1
            return SliceUtf8.countCodePoints(source, 0, startBytePosition + matcher.start()) + 1;
        }
    }

    return -1;
}
 
Example 18
Source File: Re2JRegexpReplaceLambdaFunction.java    From presto with Apache License 2.0 4 votes vote down vote up
@LiteralParameters("x")
@SqlType("varchar")
@SqlNullable
public Slice regexpReplace(
        @SqlType("varchar") Slice source,
        @SqlType(Re2JRegexpType.NAME) Re2JRegexp pattern,
        @SqlType("function(array(varchar), varchar(x))") UnaryFunctionInterface replaceFunction)
{
    // If there is no match we can simply return the original source without doing copy.
    Matcher matcher = pattern.matcher(source);
    if (!matcher.find()) {
        return source;
    }

    SliceOutput output = new DynamicSliceOutput(source.length());

    // Prepare a BlockBuilder that will be used to create the target block
    // that will be passed to the lambda function.
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);

    int groupCount = matcher.groupCount();
    int appendPosition = 0;

    do {
        int start = matcher.start();
        int end = matcher.end();

        // Append the un-matched part
        if (appendPosition < start) {
            output.writeBytes(source, appendPosition, start - appendPosition);
        }
        appendPosition = end;

        // Append the capturing groups to the target block that will be passed to lambda
        for (int i = 1; i <= groupCount; i++) {
            Slice matchedGroupSlice = matcher.group(i);
            if (matchedGroupSlice != null) {
                VARCHAR.writeSlice(blockBuilder, matchedGroupSlice);
            }
            else {
                blockBuilder.appendNull();
            }
        }
        pageBuilder.declarePositions(groupCount);
        Block target = blockBuilder.getRegion(blockBuilder.getPositionCount() - groupCount, groupCount);

        // Call the lambda function to replace the block, and append the result to output
        Slice replaced = (Slice) replaceFunction.apply(target);
        if (replaced == null) {
            // replacing a substring with null (unknown) makes the entire string null
            return null;
        }
        output.appendBytes(replaced);
    }
    while (matcher.find());

    // Append the rest of un-matched
    output.writeBytes(source, appendPosition, source.length() - appendPosition);
    return output.slice();
}
 
Example 19
Source File: InterceptOption.java    From PacketProxy with Apache License 2.0 4 votes vote down vote up
private boolean matchRegex(byte[] data) {
	Pattern pattern = Pattern.compile(this.pattern, Pattern.MULTILINE);
	Matcher matcher = pattern.matcher(new String(data));
	return matcher.find();
}
 
Example 20
Source File: AppEngineConnection.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/** Returns the contents of the title tag in the given HTML, or null if not found. */
private static String extractHtmlTitle(String html) {
  Matcher matcher = HTML_TITLE_TAG_PATTERN.matcher(html);
  return (matcher.find() ? matcher.group(1) : null);
}