Java Code Examples for com.google.re2j.Pattern#compile()

The following examples show how to use com.google.re2j.Pattern#compile() . 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: ProxyHttpTransparent.java    From PacketProxy with Apache License 2.0 6 votes vote down vote up
private HostPort parseHostName(byte[] buffer) throws Exception {
	int start = 0;
	if ((start = StringUtils.binaryFind(buffer, "Host:".getBytes())) > 0) {
		start += 5;
	} else if ((start = StringUtils.binaryFind(buffer, "host:".getBytes())) > 0) {
		start += 5;
	} else {
		throw new Exception("Host: header field is not found in beginning of 4096 bytes of packets.");
	}
	int end = StringUtils.binaryFind(buffer, "\n".getBytes(), start);
	String serverCand = new String(ArrayUtils.subarray(buffer, start, end));
	String server = "";
	int port = 80;
	Pattern pattern = Pattern.compile("^ *([^:\n\r]+)(?::([0-9]+))?");
	Matcher matcher = pattern.matcher(serverCand);
	if (matcher.find()) {
		if (matcher.group(1) != null)
			server = matcher.group(1);
		if (matcher.group(2) != null)
			port = Integer.parseInt(matcher.group(2));
	} else {
		throw new Exception("Host: header field format is not recognized.");
	}
	return new HostPort(server, port);
}
 
Example 2
Source File: ClassGraphPreprocessorTest.java    From BUILD_file_generator with Apache License 2.0 6 votes vote down vote up
/** Tests whether the black listed class names are removed from the class graph. */
@Test
public void trimRemovesBlackListedClasses() {
  MutableGraph<String> graph = newGraph();
  graph.putEdge("com.BlackList", "com.WhiteList");
  graph.putEdge("com.WhiteList", "com.BlackList");

  Pattern blackList = Pattern.compile("BlackList");

  Graph<String> actual =
      preProcessClassGraph(ImmutableGraph.copyOf(graph), EVERYTHING, blackList);

  MutableGraph<String> expected = newGraph();
  expected.addNode("com.WhiteList");

  assertEquivalent(actual, expected);
  assertThat(actual.nodes()).doesNotContain("com.BlackList");
}
 
Example 3
Source File: RegexTemplateTokens.java    From copybara with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this sequence of tokens into a regex which can be used to search a string. It
 * automatically quotes literals and represents interpolations as named groups.
 *
 * <p>It also fills groupIndexes with all the interpolation locations.
 *
 * @param regexesByInterpolationName map from group name to the regex to interpolate when the
 * group is mentioned
 * @param repeatedGroups true if a regex group is allowed to be used multiple times
 */
private Pattern buildBefore(Map<String, Pattern> regexesByInterpolationName,
    boolean repeatedGroups) throws EvalException {
  int groupCount = 1;
  StringBuilder fullPattern = new StringBuilder();
  for (Token token : tokens) {
    switch (token.getType()) {
      case INTERPOLATION:
        Pattern subPattern = regexesByInterpolationName.get(token.getValue());
        check(subPattern != null, "Interpolation is used but not defined: %s", token.getValue());
        fullPattern.append(String.format("(%s)", subPattern.pattern()));
        check(
            groupIndexes.get(token.getValue()).isEmpty() || repeatedGroups,
            "Regex group is used in template multiple times: %s",
            token.getValue());
        groupIndexes.put(token.getValue(), groupCount);
        groupCount += subPattern.groupCount() + 1;
        break;
      case LITERAL:
        fullPattern.append(Pattern.quote(token.getValue()));
        break;
    }
  }
  return Pattern.compile(fullPattern.toString(), Pattern.MULTILINE);
}
 
Example 4
Source File: RegexTemplateTokens.java    From copybara with Apache License 2.0 5 votes vote down vote up
public Replacer replacer(
    RegexTemplateTokens after, boolean firstOnly, boolean multiline,
    List<Pattern> patternsToIgnore) {
  // TODO(malcon): Remove reconstructing pattern once RE2J doesn't synchronize on matching.
  return new Replacer(Pattern.compile(before.pattern(), before.flags()), after, null, firstOnly,
                      multiline, patternsToIgnore);
}
 
Example 5
Source File: VerifyMatch.java    From copybara with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> run(Iterable<FileState> files)
    throws IOException, ValidationException {
  List<String> errors = new ArrayList<>();
  // TODO(malcon): Remove reconstructing pattern once RE2J doesn't synchronize on matching.
  Pattern batchPattern = Pattern.compile(pattern.pattern(), pattern.flags());
  for (FileState file : files) {
    String originalFileContent = new String(Files.readAllBytes(file.getPath()), UTF_8);
    if (verifyNoMatch == batchPattern.matcher(originalFileContent).find()) {
      errors.add(checkoutDir.relativize(file.getPath()).toString());
    }
  }
  return errors;
}
 
Example 6
Source File: CamelCase.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
static public String toCamelCase(String s) {
	Pattern pattern = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)");
	Matcher matcher = pattern.matcher(s);
	StringBuffer sb = new StringBuffer();
	while (matcher.find()) {
		matcher.appendReplacement(sb, toProperCase(matcher.group()));
	}
	matcher.appendTail(sb);
	return sb.toString();
}
 
Example 7
Source File: Bfg.java    From BUILD_file_generator with Apache License 2.0 5 votes vote down vote up
private static Pattern compilePattern(CmdLineParser cmdLineParser, String patternString) {
  try {
    return Pattern.compile(patternString);
  } catch (IllegalArgumentException e) {
    explainUsageErrorAndExit(cmdLineParser, String.format("Invalid regex: %s", e.getMessage()));
    return null;
  }
}
 
Example 8
Source File: XjcObjectTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshalUtf16() throws Exception {
  XjcRdeDeposit deposit = unmarshalFullDeposit();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  deposit.marshal(out, UTF_16);
  String xml = out.toString(UTF_16.toString());
  Pattern pat = Pattern.compile("^<\\?xml version=\"1\\.0\" encoding=\"UTF[-_]?16\"");
  assertWithMessage(xml).that(pat.matcher(xml).find()).isTrue();
  assertWithMessage("encode/decode didn't work: " + xml).that(xml).contains("Jane Doe");
}
 
Example 9
Source File: RegexTemplateTokens.java    From copybara with Apache License 2.0 5 votes vote down vote up
public Replacer callbackReplacer(
    RegexTemplateTokens after, AlterAfterTemplate callback, boolean firstOnly,
    boolean multiline,
    @Nullable List<Pattern> patternsToIgnore) {
  return new Replacer(Pattern.compile(before.pattern()), after, callback, firstOnly, multiline,
                      patternsToIgnore);
}
 
Example 10
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 11
Source File: XjcObjectTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshalUtf8() throws Exception {
  XjcRdeDeposit deposit = unmarshalFullDeposit();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  deposit.marshal(out, UTF_8);
  String xml = out.toString(UTF_8.toString());
  Pattern pat = Pattern.compile("^<\\?xml version=\"1\\.0\" encoding=\"UTF[-_]?8\"");
  assertWithMessage("bad xml declaration: " + xml).that(pat.matcher(xml).find()).isTrue();
  assertWithMessage("encode/decode didn't work: " + xml).that(xml).contains("Jane Doe");
}
 
Example 12
Source File: StringUtils.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
/**
 * バイナリを壊さないでパターンマッチングをする人です。疑似的に実現しているので、ascii以外は0x01にマッピングしてから比較しているのでマルチバイトの検索はできません
 *
 * @param input
 * @param regex
 * @param replace
 * @return
 */
public static byte[] pseudoBinaryPatternReplace(byte[] input, String regex, String replace) {
	byte[] input2 = input.clone();
	for(int i=0; i<input2.length; ++i) {
		if(input2[i] < 0x00) {
			input2[i] = 0x01;
		}
	}
	String pseudoString = new String(input2);
	Pattern pattern = Pattern.compile(regex);
	Matcher matcher = pattern.matcher(pseudoString);
	ByteArrayOutputStream result = new ByteArrayOutputStream();
	if(matcher.find()) {
		String matched = matcher.group(0);
		int from = pseudoString.indexOf(matched);
		//System.err.println("Match! : " + pseudoString.length() + " : " + from + " : " + regex + " : " + pseudoString);
		byte[] tmp_result = new byte[matched.length()];
		for(int i=0; i<matched.length(); ++i) {
			tmp_result[i] = input[from+i];
		}
		result.write(input,0,from);
		//System.err.println(result);
		result.write(replace.getBytes(),0,replace.getBytes().length);
		//System.err.println(result);
		result.write(input,from+matched.length(),input.length-(from+matched.length()));
		//System.err.println(result);
	}
	return result.toByteArray();
}
 
Example 13
Source File: VerifyMatch.java    From copybara with Apache License 2.0 5 votes vote down vote up
public static VerifyMatch create(Location location, String regEx, Glob paths,
    boolean verifyNoMatch, boolean alsoOnReversal, LocalParallelizer parallelizer)
    throws EvalException {
  Pattern parsed;
  try {
    parsed = Pattern.compile(regEx, Pattern.MULTILINE);
  } catch (PatternSyntaxException e) {
    throw new EvalException(location, String.format("Regex '%s' is invalid.", regEx), e);
  }
  return new VerifyMatch(parsed, verifyNoMatch, alsoOnReversal, paths, parallelizer, location);
}
 
Example 14
Source File: WildcardMatchOnPath.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public WildcardMatchOnPath(String wildcardString) {
  this.wildcardString = wildcardString;

  String regExp = wildcardString.replaceAll("\\.", "\\\\."); // Replace "." with "\.".
  regExp = regExp.replaceAll("\\*", ".*"); // Replace "*" with ".*".
  regExp = regExp.replaceAll("\\?", ".?"); // Replace "?" with ".?".

  // Compile regular expression pattern
  this.pattern = Pattern.compile(regExp);
}
 
Example 15
Source File: Re2jRegexDeserializerTest.java    From bender with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleMatch() throws FieldNotFoundException {
  List<ReFieldConfig> fields = new ArrayList<>();
  fields.add(new ReFieldConfig("foo", ReFieldType.STRING));

  Pattern p = Pattern.compile("(.*)");
  Re2jRegexDeserializer deser = new Re2jRegexDeserializer(p, fields);
  DeserializedEvent event = deser.deserialize("test i am");

  assertEquals("test i am", event.getField("foo"));
}
 
Example 16
Source File: ScannerPqact.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
Pqact(String pats, String fileout) {
  this.pats = pats;
  this.fileout = fileout;
  // System.out.println(" add <"+pats+">");
  pattern = Pattern.compile(pats);
}
 
Example 17
Source File: ScannerPqact.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
Pqact(String pats) {
  this.pats = pats;
  this.fileout = cleanup(pats);
  // System.out.println(" add <"+pats+">");
  pattern = Pattern.compile(pats);
}
 
Example 18
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 19
Source File: TestRegexp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void testOne(String ps, String match, boolean expect) {
  Pattern pattern = Pattern.compile(ps);
  Matcher matcher = pattern.matcher(match);
  assertEquals("match " + ps + " against: " + match, expect, matcher.matches());
}
 
Example 20
Source File: RegExpMatchOnName.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RegExpMatchOnName(String regExpString) {
  this.regExpString = regExpString;
  this.pattern = Pattern.compile(regExpString);
}