[英]Regular Expressions (regex) Pattern Matching
有人可以帮我理解这个程序如何计算下面给出的输出?
import java.util.regex.*;
class Demo{
public static void main(String args[]) {
String name = "abc0x12bxab0X123dpabcq0x3423arcbae0xgfaaagrbcc";
Pattern p = Pattern.compile("[a-c][abc][bca]");
Matcher m = p.matcher(name);
while(m.find()) {
System.out.println(m.start()+"\t"+m.group());
}
}
}
输出:
0 abc
18 abc
30 cba
38 aaa
43 bcc
让我们分析:
"[ac][abc][bca]"
此模式查找每组3个字母的组。
[ac]
表示首字母必须在a
和c
之间,因此它可以是a
, b
或c
[abc]
表示第二个字母必须是以下字母a
, b
或c
a
基本上[ac]
[bca]
bca [bca]
意思是第三个字母必须是b
或c
或a
,这里的命令并不重要。
您需要知道的所有内容都在官方的java正则表达式教程中http://docs.oracle.com/javase/tutorial/essential/regex/
它只是根据"[ac][abc][bca]"
指定的规则在String
搜索匹配项
0 abc --> At position 0, there is [abc].
18 abc --> Exact same thing but at position 18.
30 cba --> At position 30, there is a group of a, b and c (specified by [a-c])
38 aaa --> same as 30
43 bcc --> same as 30
请注意,计数从0开始。所以第一个字母位于第0位,第二个字母位于第1位,依此类推......
有关Regex及其使用的更多信息,请参阅: Regex的Oracle教程
此模式基本上匹配3个字符的单词,其中每个字母是a
, b
或c
。
然后它打印出每个匹配的3-char序列以及找到它的索引。
希望有所帮助。
它打印出字符串中的位置,从0开始而不是1,其中发生每个匹配。 这是第一场比赛,“abc”发生在第0位。第二场比赛“abc”发生在第18位。
基本上它匹配任何包含'a','b'和'c'的3个字符串。
模式可以写成“[ac] {3}”,你应该得到相同的结果。
让我们看一下你的源代码,因为regexp本身已经在其他答案中得到了很好的解释。
//compiles a regexp pattern, kind of make it useable
Pattern p = Pattern.compile("[a-c][abc][bca]");
//creates a matcher for your regexp pattern. This one is used to find this regexp pattern
//in your actual string name.
Matcher m = p.matcher(name);
//loop while the matcher finds a next substring in name that matches your pattern
while(m.find()) {
//print out the index of the found substring and the substring itself
System.out.println(m.start()+"\t"+m.group());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.