问题描述
这是我在给定字符串中查找ab
模式的代码。
This is my code to find "ab"
pattern in given string.
import java.util.regex.*;
public class RegExp
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher("ababbaba");
while(m.find())
{
System.out.println(m.start());
}
}
}
但我无法了解的内部工作:find()
和 start()
。
推荐答案
find
方法扫描输入序列,查找与模式匹配的下一个子序列并返回 boolean
表示失败成功。
The find
method scans the input sequence looking for the next subsequence that matches the pattern and returns a boolean
indicating the success of failure.
内部查找
方法调用搜索
方法(访问控制默认值),以便启动搜索以在给定范围内查找模式
。在每次比赛时,界限将增加,直到找到所有比赛。
Internally find
method calls the search
method (access control default) so that initiates a search to find a Pattern
within the given bounds. At each match the bound will be incremented until all the matches were found.
在 Matcher
类后面,它是一个状态机,它将保持匹配状态。
Behind the Matcher
class it's a state machine that will hold the state of the match.
另一方面 start
方法将起始索引返回为 int
最新比赛捕获的子序列。
On the other hand start
method returns the start index as int
of the subsequence captured by the latest match.
如果你想真的更深入,我建议你去看看 Matcher
class。
If you want really go deeper I suggest to review the source code of Matcher
class.
这篇关于正则表达式查找方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!