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

The following examples show how to use com.google.re2j.Matcher#groupCount() . 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: TestGhcnm.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static private int parseLine(String line) {
  int balony = 0;
  Matcher matcher = dataPattern.matcher(line);
  if (matcher.matches()) {
    for (int i = 1; i <= matcher.groupCount(); i++) {
      String r = matcher.group(i);
      if (r == null)
        continue;
      int value = (int) Long.parseLong(r.trim());
      balony += value;
    }
  } else {
    System.out.printf("Fail on %s%n", line);
  }
  return balony;
}
 
Example 2
Source File: TestRegexp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testMatch(String ps, String match, boolean expect, String[] groups) {
  Pattern pattern = Pattern.compile(ps);
  Matcher matcher = pattern.matcher(match);

  assertEquals("Match " + ps + " against " + match, expect, matcher.matches());

  if (groups != null) {
    for (int i = 1; i <= matcher.groupCount(); ++i)
      assertEquals("Wrong group " + i, groups[i - 1], matcher.group(i));
  }
}
 
Example 3
Source File: TestAccessLogParser.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testStuff() throws IOException {
  AccessLogParser p = new AccessLogParser();
  String line =
      "24.18.236.132 - - [04/Feb/2011:17:49:03 -0700] \"GET /thredds/fileServer//nexrad/level3/N0R/YUX/20110205/Level3_YUX_N0R_20110205_0011.nids \" 200 10409 \"-\" \"-\" 17";
  Matcher m = AccessLogParser.regPattern.matcher(line);
  System.out.printf("%s %s%n", m.matches(), m);
  for (int i = 0; i < m.groupCount(); i++) {
    System.out.println(" " + i + " " + m.group(i));
  }

  LogReader.Log log = p.parseLog(line);
  System.out.printf("%s%n", log);
}
 
Example 4
Source File: TestServletLogParser.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void show(String s, Pattern p) {
  System.out.println("==============================");
  Matcher m = p.matcher(s);
  System.out.printf(" match against %s = %s %n", m, m.matches());
  if (!m.matches())
    return;
  for (int i = 1; i <= m.groupCount(); i++)
    System.out.println(" " + i + " " + m.group(i));

}
 
Example 5
Source File: Re2jRegexDeserializer.java    From bender with Apache License 2.0 5 votes vote down vote up
@Override
public DeserializedEvent deserialize(String raw) {
  Matcher m = this.pattern.matcher(raw);

  if (!m.matches()) {
    throw new DeserializationException("raw event does not match string");
  }

  int groups = m.groupCount();
  JsonObject obj = new JsonObject();
  for (int i = 0; i < groups && i < fields.size(); i++) {
    String str = m.group(i + 1);

    ReFieldConfig field = this.fields.get(i);

    switch (field.getType()) {
      case BOOLEAN:
        obj.addProperty(field.getName(), Boolean.parseBoolean(str));
        break;
      case NUMBER:
        obj.addProperty(field.getName(), NumberUtils.createNumber(str));
        break;
      case STRING:
        obj.addProperty(field.getName(), str);
        break;
      default:
        obj.addProperty(field.getName(), str);
        break;
    }
  }

  return new GenericJsonEvent(obj);
}
 
Example 6
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 7
Source File: RegexTemplateTokens.java    From copybara with Apache License 2.0 4 votes vote down vote up
private String replaceLine(String line) {
  if (patternsToIgnore != null) {
    for (Pattern patternToIgnore : patternsToIgnore) {
      if (patternToIgnore.matches(line)) {
        return line;
      }
    }
  }

  Matcher matcher = before.matcher(line);
  StringBuilder sb = new StringBuilder(line.length());
  while (matcher.find()) {
    for (Collection<Integer> groupIndexes : repeatedGroups.asMap().values()) {
      // Check that all the references of the repeated group match the same string
      Iterator<Integer> iterator = groupIndexes.iterator();
      String value = matcher.group(iterator.next());
      while (iterator.hasNext()) {
        if (!value.equals(matcher.group(iterator.next()))) {
          return line;
        }
      }
    }
    String replaceTemplate;
    if (callback != null) {
      ImmutableMap.Builder<Integer, String> groupValues =
          ImmutableMap.builder();
      for (int i = 0; i <= matcher.groupCount(); i++) {
        groupValues.put(i, matcher.group(i));
      }
      replaceTemplate = callback.alter(groupValues.build(), afterReplaceTemplate);
    } else {
      replaceTemplate = afterReplaceTemplate;
    }

    matcher.appendReplacement(sb, replaceTemplate);
    if (firstOnly) {
      break;
    }
  }
  matcher.appendTail(sb);
  return sb.toString();
}