💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### [`start()`和`end()`](https://lingcoder.gitee.io/onjava8/#/book/18-Strings?id=start-%e5%92%8c-end) 在匹配操作成功之后,`start()`返回先前匹配的起始位置的索引,而`end()`返回所匹配的最后字符的索引加一的值。匹配操作失败之后(或先于一个正在进行的匹配操作去尝试)调用`start()`或`end()`将会产生`IllegalStateException`。下面的示例还同时展示了`matches()`和`lookingAt()`的用法 \[^4\]: ~~~ // strings/StartEnd.java 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 */ ~~~ 注意,`find()`可以在输入的任意位置定位正则表达式,而`lookingAt()`和`matches()`只有在正则表达式与输入的最开始处就开始匹配时才会成功。`matches()`只有在整个输入都匹配正则表达式时才会成功,而`lookingAt()`[^5](https://lingcoder.gitee.io/onjava8/#/%E6%88%91%E4%B8%8D%E7%9F%A5%E9%81%93%E4%BB%96%E4%BB%AC%E6%98%AF%E5%A6%82%E4%BD%95%E6%83%B3%E5%87%BA%E8%BF%99%E4%B8%AA%E6%96%B9%E6%B3%95%E5%90%8D%E7%9A%84%EF%BC%8C%E6%88%96%E8%80%85%E5%AE%83%E5%88%B0%E5%BA%95%E6%8C%87%E7%9A%84%E4%BB%80%E4%B9%88%E3%80%82%E8%BF%99%E5%8F%AA%E6%98%AF%E4%BB%A3%E7%A0%81%E5%AE%A1%E6%9F%A5%E5%BE%88%E9%87%8D%E8%A6%81%E7%9A%84%E5%8E%9F%E5%9B%A0%E4%B9%8B%E4%B8%80%E3%80%82)只要输入的第一部分匹配就会成功。