简体   繁体   English

正则表达式(正则表达式)模式匹配

[英]Regular Expressions (regex) Pattern Matching

Can someone please help me to understand how does this program calculate output given below? 有人可以帮我理解这个程序如何计算下面给出的输出?

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());       
        }
    }
}

OUTPUT : 输出:

0   abc
18  abc
30  cba
38  aaa
43  bcc

Lets analize: 让我们分析:
"[ac][abc][bca]"

This pattern looks for groups of 3 letters each. 此模式查找每组3个字母的组。

[ac] means that first letter has to be between a and c so it can be either a , b or c [ac]表示首字母必须在ac之间,因此它可以是abc

[abc] means that second letter has to be one of following letters a , b or c co basicly [ac] [abc]表示第二个字母必须是以下字母abc a基本上[ac]

[bca] meanst that third letter has to be either b or c or a , order rather doesnt matter here. [bca] bca [bca]意思是第三个字母必须是bca ,这里的命令并不重要。

Everything what you needs to know is in official java regex tutorial http://docs.oracle.com/javase/tutorial/essential/regex/ 您需要知道的所有内容都在官方的java正则表达式教程中http://docs.oracle.com/javase/tutorial/essential/regex/

It simply searches the String for a match according to the rules specified by "[ac][abc][bca]" 它只是根据"[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

Notice, the counting starts at 0. So the first letter is at position 0, the second ist at position 1 an so on... 请注意,计数从0开始。所以第一个字母位于第0位,第二个字母位于第1位,依此类推......

For further information about Regex and it's use see: Oracle Tutorial for Regex 有关Regex及其使用的更多信息,请参阅: Regex的Oracle教程

This pattern basically matches 3-character words where each letter is either a , b , or c . 此模式基本上匹配3个字符的单词,其中每个字母是abc

It then prints out each matching 3-char sequence along with the index at which it was found. 然后它打印出每个匹配的3-char序列以及找到它的索引。

Hope that helps. 希望有所帮助。

It is printing out the place in the string, starting with 0 instead of 1, where the occurrence of each match occurs. 它打印出字符串中的位置,从0开始而不是1,其中发生每个匹配。 That is the first match, "abc" happens in position 0. the second match "abc" happens at string position 18. 这是第一场比赛,“abc”发生在第0位。第二场比赛“abc”发生在第18位。

essentially it is matching any 3 character string that contains an 'a', 'b', and 'c'. 基本上它匹配任何包含'a','b'和'c'的3个字符串。

the pattern could be written as "[ac]{3}" and you should get the same result. 模式可以写成“[ac] {3}”,你应该得到相同的结果。

Lets look at your sourcecode, because the regexp itself was already well explained in the other answers. 让我们看一下你的源代码,因为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM