当前位置: 首页>>代码示例>>Java>>正文


Java Pattern.compile方法代码示例

本文整理汇总了Java中com.google.re2j.Pattern.compile方法的典型用法代码示例。如果您正苦于以下问题:Java Pattern.compile方法的具体用法?Java Pattern.compile怎么用?Java Pattern.compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.re2j.Pattern的用法示例。


在下文中一共展示了Pattern.compile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: trimRemovesBlackListedClasses

import com.google.re2j.Pattern; //导入方法依赖的package包/类
/** 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");
}
 
开发者ID:bazelbuild,项目名称:BUILD_file_generator,代码行数:19,代码来源:ClassGraphPreprocessorTest.java

示例2: trimRemovesTransitiveDependenciesToNonWhiteListedClasses

import com.google.re2j.Pattern; //导入方法依赖的package包/类
/**
 * Asserts that the only nodes in the trimmed graph are white listed classes and classes that the
 * white listed classes are directly dependent on.
 */
@Test
public void trimRemovesTransitiveDependenciesToNonWhiteListedClasses() {
  MutableGraph<String> graph = newGraph();
  graph.putEdge("com.WhiteList", "com.OtherList");
  graph.putEdge("com.OtherList", "com.TransitiveDependency");

  Pattern whiteList = Pattern.compile("WhiteList");

  Graph<String> actual = preProcessClassGraph(ImmutableGraph.copyOf(graph), whiteList, NOTHING);

  MutableGraph<String> expected = newGraph();
  expected.putEdge("com.WhiteList", "com.OtherList");

  assertEquivalent(actual, expected);
  assertThat(actual.nodes()).doesNotContain("com.TransitiveDependency");
}
 
开发者ID:bazelbuild,项目名称:BUILD_file_generator,代码行数:21,代码来源:ClassGraphPreprocessorTest.java

示例3: trimRemovesTransitiveDepsOfBlackListedClasses

import com.google.re2j.Pattern; //导入方法依赖的package包/类
/**
 * Tests whether black listed classes names as well as their non-whitelisted dependencies are
 * removed from the class graph. In addition to not containing the black listed class, the
 * resulting graph should also not contain nonwhite listed classes only black listed classes are
 * dependent on. For example, say we were to have the following class graph
 *
 * <p>com.Whitelist --> com.Blacklist --> com.NonWhitelist
 *
 * <p>Then the resulting class graph should only contain com.Whitelist
 */
@Test
public void trimRemovesTransitiveDepsOfBlackListedClasses() {
  MutableGraph<String> graph = newGraph();
  graph.putEdge("com.BlackList", "com.OtherList");
  graph.putEdge("com.WhiteList", "com.BlackList");

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

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

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

  assertEquivalent(actual, expected);
  assertThat(actual.nodes()).doesNotContain("com.BlackList");
  assertThat(actual.nodes()).doesNotContain("com.OtherList");
}
 
开发者ID:bazelbuild,项目名称:BUILD_file_generator,代码行数:29,代码来源:ClassGraphPreprocessorTest.java

示例4: run

import com.google.re2j.Pattern; //导入方法依赖的package包/类
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()) {
      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, result);
    }
    matcher.appendTail(sb);

    if (modified) {
      modifiedFiles.add(file);
      Files.write(file.getPath(), sb.toString().getBytes(UTF_8));
    }
  }
  return modifiedFiles;
}
 
开发者ID:google,项目名称:copybara,代码行数:32,代码来源:TodoReplace.java

示例5: extractIDString

import com.google.re2j.Pattern; //导入方法依赖的package包/类
/**
 * Extract Date + ID + No
 * Ex: " 15/02/14(六)07:14:32 ID:F.OqpZFA No.6135732"
 * @return Post
 */
private Post extractIDString(Post post, TextNode node) {
    Pattern r = Pattern.compile("(\\d{2})/(\\d{2})/(\\d{2}).+?(\\d{2}):(\\d{2}):(\\d{2}) ID:([\\./0-9A-Za-z]+?) No\\.(\\d+)");
    Matcher m = r.matcher(node.text());
    if (m.find()) {
        Integer Y = Integer.parseInt(m.group(1)) + 2000, //year
                M = Integer.parseInt(m.group(2)) - 1, //month
                D = Integer.parseInt(m.group(3)), //day
                H = Integer.parseInt(m.group(4)), //hours
                I = Integer.parseInt(m.group(5)), //minutes
                S = Integer.parseInt(m.group(6)); //seconds
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Asia/Taipei"));
        cal.set(Y, M, D, H, I, S);
        post.date = cal;

        post.tripId = m.group(7);
        post.no = m.group(8);
    }

    return post;
}
 
开发者ID:touhonoob,项目名称:KomiReader,代码行数:26,代码来源:KomicaScraper.java

示例6: compilePattern

import com.google.re2j.Pattern; //导入方法依赖的package包/类
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;
  }
}
 
开发者ID:bazelbuild,项目名称:BUILD_file_generator,代码行数:9,代码来源:Bfg.java

示例7: testSingleMatch

import com.google.re2j.Pattern; //导入方法依赖的package包/类
@Test
public void testSingleMatch() {
  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"));
}
 
开发者ID:Nextdoor,项目名称:bender,代码行数:12,代码来源:Re2jRegexDeserializerTest.java

示例8: testNoMatches

import com.google.re2j.Pattern; //导入方法依赖的package包/类
@Test(expected = DeserializationException.class)
public void testNoMatches() {
  List<ReFieldConfig> fields = new ArrayList<>();
  fields.add(new ReFieldConfig("foo", ReFieldType.STRING));

  Pattern p = Pattern.compile("(test i am)");
  Re2jRegexDeserializer deser = new Re2jRegexDeserializer(p, fields);
  DeserializedEvent event = deser.deserialize("i am a test");
}
 
开发者ID:Nextdoor,项目名称:bender,代码行数:10,代码来源:Re2jRegexDeserializerTest.java

示例9: run

import com.google.re2j.Pattern; //导入方法依赖的package包/类
@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;
}
 
开发者ID:google,项目名称:copybara,代码行数:15,代码来源:VerifyMatch.java

示例10: create

import com.google.re2j.Pattern; //导入方法依赖的package包/类
public static VerifyMatch create(Location location, String regEx, Glob paths,
    boolean verifyNoMatch, 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, paths, parallelizer);
}
 
开发者ID:google,项目名称:copybara,代码行数:11,代码来源:VerifyMatch.java

示例11: invoke

import com.google.re2j.Pattern; //导入方法依赖的package包/类
public Transformation invoke(MetadataModule self, String regex, String replacement,
    Location location)
    throws EvalException {
  Pattern pattern;
  try {
    pattern = Pattern.compile(regex, Pattern.MULTILINE);
  } catch (PatternSyntaxException e) {
    throw new EvalException(location, "Invalid regex expression: " + e.getMessage());
  }
  return new Scrubber(pattern, replacement);
}
 
开发者ID:google,项目名称:copybara,代码行数:12,代码来源:MetadataModule.java

示例12: buildBefore

import com.google.re2j.Pattern; //导入方法依赖的package包/类
/**
 * 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.
 *
 * @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
 */
Pattern buildBefore(Map<String, Pattern> regexesByInterpolationName, boolean repeatedGroups)
    throws EvalException {
  int groupCount = 1;
  StringBuilder fullPattern = new StringBuilder();
  for (Token token : tokens) {
    switch (token.type) {
      case INTERPOLATION:
        Pattern subPattern = regexesByInterpolationName.get(token.value);
        if (subPattern == null) {
          throw new EvalException(
              location, "Interpolation is used but not defined: " + token.value);
        }
        fullPattern.append(String.format("(%s)", subPattern.pattern()));
        if (groupIndexes.get(token.value).size() > 0 && !repeatedGroups) {
          throw new EvalException(
              location, "Regex group is used in template multiple times: " + token.value);
        }
        groupIndexes.put(token.value, groupCount);
        groupCount += subPattern.groupCount() + 1;
        break;
      case LITERAL:
        fullPattern.append(Pattern.quote(token.value));
        break;
      default:
        throw new IllegalStateException(token.type.toString());
    }
  }
  return Pattern.compile(fullPattern.toString(), Pattern.MULTILINE);
}
 
开发者ID:google,项目名称:copybara,代码行数:38,代码来源:TemplateTokens.java

示例13: testMarshalUtf8

import com.google.re2j.Pattern; //导入方法依赖的package包/类
@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");
}
 
开发者ID:google,项目名称:nomulus,代码行数:11,代码来源:XjcObjectTest.java

示例14: testMarshalUtf16

import com.google.re2j.Pattern; //导入方法依赖的package包/类
@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");
}
 
开发者ID:google,项目名称:nomulus,代码行数:11,代码来源:XjcObjectTest.java

示例15: extractListFromFile

import com.google.re2j.Pattern; //导入方法依赖的package包/类
/**
 * Helper method to extract list from file
 *
 * @param filename
 * @return ImmutableList<String>
 */
private static ImmutableList<String> extractListFromFile(String filename) {
  String fileContents = readResourceUtf8(ExportConstantsTest.class, filename);
  final Pattern stripComments = Pattern.compile("\\s*#.*$");
  return Streams.stream(Splitter.on('\n').split(fileContents.trim()))
      .map(line -> stripComments.matcher(line).replaceFirst(""))
      .collect(toImmutableList());
}
 
开发者ID:google,项目名称:nomulus,代码行数:14,代码来源:ExportConstantsTest.java


注:本文中的com.google.re2j.Pattern.compile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。