Java - 正则表达式

时间:2014-12-10 12:48:44

标签: java regex

在Java中,我目前正在学习正则表达式语法,但我并不真正理解RE模式...

我所知道的是模式具有组长度,而下面的字符串模式的长度为3。

import java.util.regex.*;

public class RE {
    public static void main(String[] args){
        String line = "Foo123";
        String pattern = "(.*)(\\d+)(.*)"; //RE Syntax I get stuck on.

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);

        if (m.find()) {
            System.out.println(m.group(0));
            System.out.println(m.group(1));
            System.out.println(m.group(2));
            System.out.println(m.group(3));
        }
    }
}

如果有人能向我解释这个表达式对多个群体做了什么等等,我会是这样的...

4 个答案:

答案 0 :(得分:3)

组0包含整个匹配,组1,2,3包含相应的捕获字符。

输入字符串:Foo123

正则表达式:(.*)(\d+)(.*)

第一个捕获组中的第一个.*匹配到最后一个捕获的所有字符。然后它回溯直到找到一个数字。回溯的原因是为了找到匹配。并且组2将捕获相应的数字(最后一个数字)。所有数字后面都没有任何内容,所以你在第3组中有一个空字符串。

DEMO

答案 1 :(得分:1)

以下是解释:

(       : start capture group 1
    .*  : 0 or more any character
)       : end group
(       : start capture group 2
    \\d+: 1 or more digit
)       : end group
(       : start capture group 3
    .*  : 0 or more any character
)       : end group

此正则表达式匹配例如:

  • 123
  • abc456kljh
  • :.?222

答案 2 :(得分:1)

String line = "Foo123";
String pattern = "(.*)(\\d+)(.*)"; 
// (take any character - zero or more) // (digits one or more) // (take any character - zero or more)

因此,在上述情况下,我们捕获了3组。一个任何字符为零或更多(贪婪 - can read at this link),那么我们有\ d pattern +的数字对应一个或多个。

答案 3 :(得分:0)

<强> (.)(\\d+)(.)

  

您可以将鼠标悬停在正则表达式上,然后您将获得该部分的说明。

1st Capturing group (.*)
  .* matches any character (except newline)
  Quantifier: * Between zero and unlimited times, as many times as possible
2nd Capturing group (\d+)
  \\ matches the character \ literally
  d+ matches the character d literally (case sensitive)
  Quantifier: + Between one and unlimited times, as many times as possible
3rd Capturing group (.*)
  .* matches any character (except newline)
  Quantifier: * Between zero and unlimited times, as many times as possible