gpt4 book ai didi

java - 正则表达式(regex)模式匹配

转载 作者:行者123 更新时间:2023-12-02 05:58:54 25 4
gpt4 key购买 nike

有人可以帮我了解一下该程序如何计算下面给出的输出吗?

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

最佳答案

它仅根据String指定的规则在"[a-c][abc][bca]"中搜索匹配项


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,第二个ist位于位置1,依此类推...

有关正则表达式及其使用的更多信息,请参见: Oracle Tutorial for Regex

关于java - 正则表达式(regex)模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com