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

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

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

Matcher.search介绍

[英]Initiates a search to find a Pattern within the given bounds. The groups are filled with default values and the match of the root of the state machine is called. The state machine will hold the state of the match as it proceeds in this matcher. Matcher.from is not set here, because it is the "hard" boundary of the start of the search which anchors will set to. The from param is the "soft" boundary of the start of the search, meaning that the regex tries to match at that index but ^ won't match there. Subsequent calls to the search methods start at a new "soft" boundary which is the end of the previous match.
[中]启动搜索以查找给定边界内的模式。使用默认值填充组,并调用状态机根的匹配。当状态机在此匹配器中进行匹配时,它将保持匹配的状态。匹配器。这里不设置from,因为它是锚定将设置为的搜索开始的“硬”边界。from参数是搜索开始的“软”边界,这意味着正则表达式试图在该索引处匹配,但在该索引处^将不匹配。对搜索方法的后续调用从新的“软”边界开始,该边界是上一个匹配的结束。

代码示例

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Attempts to find the next subsequence of the input sequence that matches
 * the pattern.
 *
 * <p> This method starts at the beginning of this matcher's region, or, if
 * a previous invocation of the method was successful and the matcher has
 * not since been reset, at the first character not matched by the previous
 * match.
 *
 * <p> If the match succeeds then more information can be obtained via the
 * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
 *
 * @return  <tt>true</tt> if, and only if, a subsequence of the input
 *          sequence matches this matcher's pattern
 */
public boolean find() {
  int nextSearchIndex = last;
  if (nextSearchIndex == first)
    nextSearchIndex++;
  // If next search starts before region, start it at region
  if (nextSearchIndex < from)
    nextSearchIndex = from;
  // If next search starts beyond region then it fails
  if (nextSearchIndex > to) {
    for (int i = 0; i < groups.length; i++)
      groups[i] = -1;
    return false;
  }
  return search(nextSearchIndex);
}

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Attempts to find the next subsequence of the input sequence that matches
 * the pattern.
 *
 * <p> This method starts at the beginning of this matcher's region, or, if
 * a previous invocation of the method was successful and the matcher has
 * not since been reset, at the first character not matched by the previous
 * match.
 *
 * <p> If the match succeeds then more information can be obtained via the
 * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
 *
 * @return  <tt>true</tt> if, and only if, a subsequence of the input
 *          sequence matches this matcher's pattern
 */
public boolean find() {
  int nextSearchIndex = last;
  if (nextSearchIndex == first)
    nextSearchIndex++;
  // If next search starts before region, start it at region
  if (nextSearchIndex < from)
    nextSearchIndex = from;
  // If next search starts beyond region then it fails
  if (nextSearchIndex > to) {
    for (int i = 0; i < groups.length; i++)
      groups[i] = -1;
    return false;
  }
  return search(nextSearchIndex);
}

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

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 at java.lang.String.charAt(String.java:695)
 at java.util.regex.Pattern$Slice.match(Pattern.java:3867)
 at java.util.regex.Pattern$GroupCurly.match0(Pattern.java:4360)
 at java.util.regex.Pattern$GroupCurly.match0(Pattern.java:4354)
 at java.util.regex.Pattern$GroupCurly.match(Pattern.java:4304)
 at java.util.regex.Pattern$Slice.match(Pattern.java:3870)
 at java.util.regex.Pattern$GroupTail.match(Pattern.java:4615)
 at java.util.regex.Pattern$Curly.match1(Pattern.java:4185)
 at java.util.regex.Pattern$Curly.match(Pattern.java:4134)
 at java.util.regex.Pattern$GroupHead.match(Pattern.java:4556)
 at java.util.regex.Pattern$Begin.match(Pattern.java:3472)
 at java.util.regex.Matcher.search(Matcher.java:1199)
 at java.util.regex.Matcher.find(Matcher.java:592)
 at Test.main(Test.java:9)

代码示例来源:origin: org.apidesign.bck2brwsr/emul

/**
 * Resets this matcher and then attempts to find the next subsequence of
 * the input sequence that matches the pattern, starting at the specified
 * index.
 *
 * <p> If the match succeeds then more information can be obtained via the
 * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent
 * invocations of the {@link #find()} method will start at the first
 * character not matched by this match.  </p>
 *
 * @throws  IndexOutOfBoundsException
 *          If start is less than zero or if start is greater than the
 *          length of the input sequence.
 *
 * @return  <tt>true</tt> if, and only if, a subsequence of the input
 *          sequence starting at the given index matches this matcher's
 *          pattern
 */
public boolean find(int start) {
  int limit = getTextLength();
  if ((start < 0) || (start > limit))
    throw new IndexOutOfBoundsException("Illegal start index");
  reset();
  return search(start);
}

代码示例来源:origin: jtulach/bck2brwsr

/**
 * Resets this matcher and then attempts to find the next subsequence of
 * the input sequence that matches the pattern, starting at the specified
 * index.
 *
 * <p> If the match succeeds then more information can be obtained via the
 * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent
 * invocations of the {@link #find()} method will start at the first
 * character not matched by this match.  </p>
 *
 * @throws  IndexOutOfBoundsException
 *          If start is less than zero or if start is greater than the
 *          length of the input sequence.
 *
 * @return  <tt>true</tt> if, and only if, a subsequence of the input
 *          sequence starting at the given index matches this matcher's
 *          pattern
 */
public boolean find(int start) {
  int limit = getTextLength();
  if ((start < 0) || (start > limit))
    throw new IndexOutOfBoundsException("Illegal start index");
  reset();
  return search(start);
}

相关文章