Java - 字符串可以使用正则表达式进行拆分,但正则表达式匹配返回false

时间:2017-12-07 14:48:41

标签: java regex

我正在尝试使用以下示例拆分以下字符串: Team A 2-3 Team B 这是一个字符串,两侧有团队名称,中间有分数。我需要分别获得球队的名字和分数。到目前为止,这是我的代码:

    String homeTeam;
    String awayTeam;
    int homeGoal = 0;
    int awayGoal = 0;
    String input = "Team A 2-3 Team B";
    Scanner s = new Scanner(input).useDelimiter("-");
    String[] teamNames = input.split(" (\\d+)-(\\d+) ");
    homeTeam = teamNames[0];
    awayTeam = teamNames[1];
    for(int i = 0; i < teamNames.length; i++) {
    System.out.println(teamNames[i]);
    }
    s.findInLine("(\\d+)-(\\d+)");
    MatchResult result = s.match();
    for (int i=1; i<=result.groupCount(); i++)
    System.out.println(result.group(i));
    s.close();

此输出:

Team A
Team B
2
3

应该如此。

但是,由于还有另一种情况,即匹配尚未进行Team A Team B。在这种情况下,2个团队名称由2个空格分隔。上面使用的正则表达式不再有效,所以我有2个案例。这是我的整体代码:

        String homeTeam;
        String awayTeam;
        int homeGoal = 0;
        int awayGoal = 0;
        String input = "Team A 2-3 Team B";
        Scanner s = new Scanner(input).useDelimiter("-");

    boolean b = Pattern.matches(" (\\d+)-(\\d+) ", input);
    if (b){
        String[] teamNames = input.split(" (\\d+)-(\\d+) ");
        homeTeam = teamNames[0];
        awayTeam = teamNames[1];
        for(int i = 0; i < teamNames.length; i++) {
            System.out.println(teamNames[i]);
        }
        s.findInLine("(\\d+)-(\\d+)");
        MatchResult result = s.match();
        for (int i=1; i<=result.groupCount(); i++)
            System.out.println(result.group(i));
        s.close();
    } else {
        String[] teamNames = input.split("  ");
        homeTeam = teamNames[0];
        awayTeam = teamNames[1];
        for(int i = 0; i < teamNames.length; i++) {
            System.out.println(teamNames[i]);
        }
    }
    System.out.println(b);

我得到的问题是,即使是字符串

,b也会返回false
Team A 2-3 Team B

所以只有这个代码才会返回其他情况。

我用符合他们自己的模式的字符串单独测试了两个部分并且它们工作正常,但是当我像这样组合时没有。

2 个答案:

答案 0 :(得分:1)

代码

See regex in use here

([\w ]*?)\s+(?:(\d+)-(\d+))?\s+([\w ]*)

代码

See code in use here

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        final String regex = "([\\w ]*?)\\s+(?:(\\d+)-(\\d+))?\\s+([\\w ]*)";
        final String[] strings = {"Team A 2-3 Team B", "Team A  Team B"};

        final Pattern pattern = Pattern.compile(regex);

        for (String string : strings) {
            Matcher matcher = pattern.matcher(string);
            if(matcher.find()) {
                System.out.println("Home team: " + matcher.group(1));
                if(matcher.group(2) != null) {
                    System.out.println("Home goal: " + matcher.group(2));
                    System.out.println("Away goal: " + matcher.group(3));
                }
                System.out.println("Away team: " + matcher.group(4));
                System.out.println("\n");
            }
        }
    }
}

说明

  • ([\w ]*?)捕获集合中的任何字符(单词字符或空格)任意次,但尽可能少地捕获到捕获组1
  • \s+匹配一个或多个空白字符
  • (?:(\d+)-(\d+))?匹配以下零次或一次
    • (\d+)将一个或多个数字捕获到捕获组2
    • -字面上匹配连字符-
    • (\d+)将一个或多个数字捕获到捕获组3
  • \s+匹配一个或多个空白字符
  • ([\w ]*)将任意集合(字符或空格)中的任何字符捕获到捕获组4中

答案 1 :(得分:-1)

如果字符串中间的分数可能不存在,您可以修改表达式以表示:

    boolean b = Pattern.matches(" ((\\d+)-(\\d+))? ", input);
    ...
    String[] teamNames = input.split(" ((\\d+)-(\\d+))? ");