strings - Macher - start() and end() and related methods

start

public int start()

Returns the start index of the previous match.

Specified by:

start in interface MatchResult

Returns:

The index of the first character matched

Throws:

IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

 

end

public int end()

Returns the offset after the last character matched.

Return the index of the last character matched, plus one.

Specified by:

end in interface MatchResult

Returns:

The offset after the last character matched

Throws:

IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

find

public boolean find()

Attempts to find the next subsequence of the input sequence that matches the pattern.

This method will locate the regular expression anywhere in the input.

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.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:

true if, and only if, a subsequence of the input sequence matches this matcher's pattern

for example:

// strings/StartEnd.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.regex.*;

public class StartEnd {
  public static String input =
      "As long as there is injustice, whenever a\n"
          + "Targathian baby cries out, wherever a distress\n"
          + "signal sounds among the stars "
          + "... We'll be there.\n"
          + "This fine ship, and this fine crew ...\n"
          + "Never give up! Never surrender!";

  private static class Display {
    private boolean regexPrinted = false;
    private String regex;

    Display(String regex) {
      this.regex = regex;
    }

    void display(String message) {
      if (!regexPrinted) {
        System.out.println(regex);
        regexPrinted = true;
      }
      System.out.println(message);
    }
  }

  static void examine(String s, String regex) {
    Display d = new Display(regex);
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(s);
    while (m.find()) {
      d.display("find() '" + m.group() + "' start = " + m.start() + " end = " + m.end());
    }
    if (m.lookingAt()) { // No reset() necessary
      d.display("lookingAt() start = " + m.start() + " end = " + m.end());
    }
    if (m.matches()) { // No reset() necessary
      d.display("matches() start = " + m.start() + " end = " + m.end());
    }
  }

  public static void main(String[] args) {
    for (String in : input.split("\n")) {
      System.out.println("input : " + in);
      for (String regex : new String[] {"\\w*ere\\w*", "\\w*ever", "T\\w+", "Never.*?!"}) {
        examine(in, regex);
      }
    }
  }
}
/* Output:
input : As long as there is injustice, whenever a
\w*ere\w*
find() 'there' start = 11 end = 16
\w*ever
find() 'whenever' start = 31 end = 39
input : Targathian baby cries out, wherever a distress
\w*ere\w*
find() 'wherever' start = 27 end = 35
\w*ever
find() 'wherever' start = 27 end = 35
T\w+
find() 'Targathian' start = 0 end = 10
lookingAt() start = 0 end = 10
input : signal sounds among the stars ... We'll be there.

\w*ere\w*
find() 'there' start = 43 end = 48
input : This fine ship, and this fine crew ...
T\w+
find() 'This' start = 0 end = 4
lookingAt() start = 0 end = 4
input : Never give up! Never surrender!
\w*ever
find() 'Never' start = 0 end = 5
find() 'Never' start = 15 end = 20
lookingAt() start = 0 end = 5
Never.*?!
find() 'Never give up!' start = 0 end = 14
find() 'Never surrender!' start = 15 end = 31
lookingAt() start = 0 end = 14
matches() start = 0 end = 31
*/

lookingAt

public boolean lookingAt()

Attempts to match the input sequence, starting at the beginning of the region, against the pattern.

Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:

true if, and only if, a prefix of the input sequence matches this matcher's pattern

 

group

public String group()

Returns the input subsequence matched by the previous match.

For a matcher m with input sequence s, the expressions m.group() and s.substring(m.start(), m.end()) are equivalent.

Note that some patterns, for example a*, match the empty string. This method will return the empty string when the pattern successfully matches the empty string in the input.

Specified by:

group in interface MatchResult

Returns:

The (possibly empty) subsequence matched by the previous match, in string form

Throws:

IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/StartEnd.java

3. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#start--

4. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#lookingAt--

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值