java.util.regex.Matcher.start()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(210)

本文整理了Java中java.util.regex.Matcher.start()方法的一些代码示例,展示了Matcher.start()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Matcher.start()方法的具体详情如下:
包路径:java.util.regex.Matcher
类名称:Matcher
方法名:start

Matcher.start介绍

[英]Returns the index of the first character of the text that matched the whole regular expression.
[中]返回与整个正则表达式匹配的文本的第一个字符的索引。

代码示例

代码示例来源:origin: jenkinsci/jenkins

public SubText(Matcher m, int textOffset) {
  start = m.start() + textOffset;
  end   = m.end() + textOffset;
  int cnt = m.groupCount();
  groups = new int[cnt*2];
  for( int i=0; i<cnt; i++ ) {
    groups[i*2  ] = m.start(i+1) + textOffset;
    groups[i*2+1] = m.end(i+1) + textOffset;
  }
}

代码示例来源:origin: jenkinsci/jenkins

String applyPatternRepeatedly(@Nonnull Pattern pattern, @Nonnull String filename,
               @Nonnull Function<String, Character> converter) {
  StringBuilder id = new StringBuilder();
  int beginIndex = 0;
  Matcher matcher = pattern.matcher(filename);
  while (matcher.find()) {
    String group = matcher.group();
    id.append(filename, beginIndex, matcher.start());
    id.append(converter.apply(group));
    beginIndex = matcher.end();
  }
  id.append(filename.substring(beginIndex));
  return id.toString();
}

代码示例来源:origin: aragozin/jvm-tools

private void processStatePattern() {
  String pattern = matcher.group(1);
  int off = "#STATE=".length();
  Op op = new Op();
  op.toc = TokenType.STATE_PATTERN;
  op.rank = -1;
  op.body = pattern.substring(off);
  op.offset = matcher.start() + off;
  
  pushToken(op);
}

代码示例来源:origin: org.freemarker/freemarker

MatchWithGroups(String input, Matcher matcher) {
  matchedInputPart = input.substring(matcher.start(), matcher.end());
  final int grpCount = matcher.groupCount() + 1;
  groupsSeq = new SimpleSequence(grpCount);
  for (int i = 0; i < grpCount; i++) {
    groupsSeq.add(matcher.group(i));
  }
}

代码示例来源:origin: stackoverflow.com

public static void printMatches(String text, String regex) {
  Pattern pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher(text);
  // Check all occurrences
  while (matcher.find()) {
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end());
    System.out.println(" Found: " + matcher.group());
  }
}

代码示例来源:origin: aragozin/jvm-tools

private void processPattern() {
  String pattern = matcher.group(1);
  Op op = new Op();
  op.toc = TokenType.PATTERN;
  op.rank = -1;
  op.body = pattern;
  op.offset = matcher.start();
  
  pushToken(op);
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
protected Result[] run(String value) {
  final Matcher matcher = pattern.matcher(value);
  if (!matcher.find() || matcher.groupCount() == 0 || matcher.start(1) == -1 || matcher.end(1) == -1) {
    return null;
  }
  return new Result[] { new Result(value.substring(matcher.start(1), matcher.end(1)), matcher.start(1), matcher.end(1)) };
}

代码示例来源:origin: aragozin/jvm-tools

private void processOp(TokenType tt, int rank) {
  Op op = new Op();
  op.toc = tt;
  op.rank = rank;
  op.body = matcher.group(1);
  op.offset = matcher.start();
  
  pushToken(op);            
}

代码示例来源:origin: google/j2objc

@Override
public void describeTo(Description description) {
  java.util.regex.Matcher arg = ARG_PATTERN.matcher(descriptionTemplate);
  
  int textStart = 0;
  while (arg.find()) {
    description.appendText(descriptionTemplate.substring(textStart, arg.start()));
    description.appendValue(values[parseInt(arg.group(1))]);
    textStart = arg.end();
  }
  
  if (textStart < descriptionTemplate.length()) {
    description.appendText(descriptionTemplate.substring(textStart));
  }
}

代码示例来源:origin: aragozin/jvm-tools

private void processUniverse() {
  String pattern = matcher.group(1);
  Op op = new Op();
  op.toc = TokenType.UNIVERSE;
  op.rank = -1;
  op.body = pattern;
  op.offset = matcher.start();
  
  pushToken(op);
}

代码示例来源:origin: amitshekhariitbhu/Android-Debug-Database

private int indexOfRegex(String regex, String string) {
  Pattern pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher(string);
  return matcher.find() ? matcher.start() : -1;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Expand URI template variables in the target URL with either model
 * attribute values or as a fallback with URI variable values from the
 * current request. Values are encoded.
 */
protected StringBuilder expandTargetUrlTemplate(String targetUrl,
    Map<String, Object> model, Map<String, String> uriVariables) {
  Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
  boolean found = matcher.find();
  if (!found) {
    return new StringBuilder(targetUrl);
  }
  StringBuilder result = new StringBuilder();
  int endLastMatch = 0;
  while (found) {
    String name = matcher.group(1);
    Object value = (model.containsKey(name) ? model.get(name) : uriVariables.get(name));
    Assert.notNull(value, () -> "No value for URI variable '" + name + "'");
    result.append(targetUrl.substring(endLastMatch, matcher.start()));
    result.append(encodeUriVariable(value.toString()));
    endLastMatch = matcher.end();
    found = matcher.find();
  }
  result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
  return result;
}

代码示例来源:origin: NanoHttpd/nanohttpd

private Pattern createUriPattern() {
  String patternUri = uri;
  Matcher matcher = PARAM_PATTERN.matcher(patternUri);
  int start = 0;
  while (matcher.find(start)) {
    uriParams.add(patternUri.substring(matcher.start() + 1, matcher.end()));
    patternUri = new StringBuilder(patternUri.substring(0, matcher.start()))//
        .append(PARAM_MATCHER)//
        .append(patternUri.substring(matcher.end())).toString();
    start = matcher.start() + PARAM_MATCHER.length();
    matcher = PARAM_PATTERN.matcher(patternUri);
  }
  return Pattern.compile(patternUri);
}

代码示例来源:origin: hankcs/HanLP

public static FeatureTemplate create(String template)
{
  FeatureTemplate featureTemplate = new FeatureTemplate();
  featureTemplate.delimiterList = new LinkedList<String>();
  featureTemplate.offsetList = new ArrayList<int[]>(3);
  featureTemplate.template = template;
  Matcher matcher = pattern.matcher(template);
  int start = 0;
  while (matcher.find())
  {
    featureTemplate.delimiterList.add(template.substring(start, matcher.start()));
    start = matcher.end();
    featureTemplate.offsetList.add(new int[]{Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))});
  }
  return featureTemplate;
}

代码示例来源:origin: org.hamcrest/hamcrest-all

@Override
public void describeTo(Description description) {
  java.util.regex.Matcher arg = ARG_PATTERN.matcher(descriptionTemplate);
  
  int textStart = 0;
  while (arg.find()) {
    description.appendText(descriptionTemplate.substring(textStart, arg.start()));
    description.appendValue(values[parseInt(arg.group(1))]);
    textStart = arg.end();
  }
  
  if (textStart < descriptionTemplate.length()) {
    description.appendText(descriptionTemplate.substring(textStart));
  }
}

代码示例来源:origin: languagetool-org/languagetool

private static String findFirstRegexMatch(String regex, String stringToSearch) {
 String result = "";
 Pattern pattern = Pattern.compile(regex);
 Matcher stringToSearchMatcher = pattern.matcher(stringToSearch);
 if (stringToSearchMatcher.find()) {
  result = stringToSearch.substring(stringToSearchMatcher.start(), stringToSearchMatcher.end());
 }
 return result;
}

代码示例来源:origin: Graylog2/graylog2-server

private static Set<Range<Integer>> extractRange(List<String> highlights) {
    final ImmutableSet.Builder<Range<Integer>> builder = ImmutableSet.builder();
    highlights.forEach(highlight -> {
      final Matcher matcher = highlightPattern.matcher(highlight);
      Integer count = -1;
      while (matcher.find()) {
        count++;
        final Integer start = matcher.start() - count * (startTokenLength + endTokenLength);
        final Integer end = start + (matcher.end(1) - matcher.start(1));
        builder.add(Range.closed(start, end));
      }
    });
    return builder.build();
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Replace URI template variables in the target URL with encoded model
 * attributes or URI variables from the current request. Model attributes
 * referenced in the URL are removed from the model.
 * @param targetUrl the redirect URL
 * @param model a Map that contains model attributes
 * @param currentUriVariables current request URI variables to use
 * @param encodingScheme the encoding scheme to use
 * @throws UnsupportedEncodingException if string encoding failed
 */
protected StringBuilder replaceUriTemplateVariables(
    String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme)
    throws UnsupportedEncodingException {
  StringBuilder result = new StringBuilder();
  Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl);
  int endLastMatch = 0;
  while (matcher.find()) {
    String name = matcher.group(1);
    Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name));
    if (value == null) {
      throw new IllegalArgumentException("Model has no value for key '" + name + "'");
    }
    result.append(targetUrl.substring(endLastMatch, matcher.start()));
    result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme));
    endLastMatch = matcher.end();
  }
  result.append(targetUrl.substring(endLastMatch, targetUrl.length()));
  return result;
}

代码示例来源:origin: google/google-java-format

boolean tryConsumeRegex(Pattern pattern) {
 Matcher matcher = pattern.matcher(remaining);
 if (!matcher.find()) {
  return false;
 }
 checkArgument(matcher.start() == 0);
 toConsume = matcher.end();
 return true;
}

代码示例来源:origin: k9mail/k-9

public static String extractMessageId(final String text) {
  Matcher matcher = MESSAGE_ID.matcher(text);
  if (matcher.find()) {
    return text.substring(matcher.start(), matcher.end());
  }
  return null;
}

相关文章