正则表达式匹配数字或没有

时间:2014-09-23 21:40:05

标签: java regex capturing-group

我需要得到一个可以匹配这样的正则表达式:

1234 <CIRCLE> 12 12 12 </CIRCLE>

1234 <RECTANGLE> 12 12 12 12 </RECTANGLE>

我来写这个正则表达式:

(\\d+?) <([A-Z]+?)> (\\d+?) (\\d+?) (\\d+?) (\\d*)? (</[A-Z]+?>)

当我尝试匹配矩形时,它可以正常工作,但它对圆圈不起作用

问题是我的第五组没有捕获虽然应该是??

5 个答案:

答案 0 :(得分:1)

尝试

(\\d+?) <([A-Z]+?)> (\\d+?) (\\d+?) (\\d+?) (\\d+ )?(</[A-Z]+?>)

(我更改了最后一个“\ d”组以使空格也可选。)

答案 1 :(得分:0)

这是因为只有(\\d*)?部分是可选的,但前后的空格是必需的,所以如果找不到最后(\\d*),你最终需要两个空格。尝试使用像

这样的东西
(\\d+?) <([A-Z]+?)> (:?(\\d+?) ){3,4}(</[A-Z]+?>)

哦,如果你想确保结束标记与开放标记相同,你可以使用像\\1这样的组引用代表第一组的匹配。所以可能会将你的正则表达式更新为

(\\d+?) <([A-Z]+?)> (:?(\\d+?) ){3,4}(</\\2>)
//        ^^^^^^^-----------------------^^^ 
//        group 2                       here value need to match one from group 2

答案 2 :(得分:0)

仅针对数字的解决方案:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;

public class Q26005150
{
    private static final Pattern P = Pattern.compile("(\\d+)");

    public static void main(String[] args)
    {
        final String s1 = "1234 <CIRCLE> 12 12 12 </CIRCLE>";
        final String s2 = "1234 <RECTANGLE> 12 12 12 12 </RECTANGLE>";

        final List<Integer> l1 = getAllMatches(s1);
        final List<Integer> l2 = getAllMatches(s2);

        System.out.println("l1 = " + l1);
        System.out.println("l2 = " + l2);
    }

    private static List<Integer> getAllMatches(@Nonnull final String s)
    {
        final Matcher m = P.matcher(s);
        final List<Integer> matches = new ArrayList<Integer>();
        while(m.find())
        {
            matches.add(Integer.valueOf(m.group(1)));
        }
        return matches;
    }
}

输出:

l1 = [1234, 12, 12, 12]
l2 = [1234, 12, 12, 12, 12]

Answer on GitHub

Stackoverflow GitHub repository

答案 3 :(得分:0)

数字和标签的解决方案

private static final Pattern P = Pattern.compile("(<\/?(\w+)>|(\d+))");

public static void main(String[] args)
{
    final String s1 = "1234 <CIRCLE> 12 12 12 </CIRCLE>";
    final String s2 = "1234 <RECTANGLE> 12 12 12 12 </RECTANGLE>";

    final List<String> l1 = getAllMatches(s1);
    final List<String> l2 = getAllMatches(s2);

    System.out.println("l1 = " + l1);
    System.out.println("l2 = " + l2);
}

private static List<String> getAllMatches(@Nonnull final String s)
{
    final Matcher m = P.matcher(s);
    final List<String> matches = new ArrayList<String>();
    while(m.find())
    {
        final String match = m.group(1);
        matches.add(match);
    }
    return matches;
}

输出:

l1 = [1234, <CIRCLE>, 12, 12, 12, </CIRCLE>]
l2 = [1234, <RECTANGLE>, 12, 12, 12, 12, </RECTANGLE>]

Answer on GitHub

Stackoverflow GitHub repository

答案 4 :(得分:-1)

假设“&lt;”之间的标签&安培; “&gt;” 中必须匹配,两者之间的数字相同 使用这种模式

^\d+\s<([A-Z]+)>\s(\d+\s)(\2)+<\/(\1)>$  

Demo

或者如果中间的数字不必相同或可选:

^\d+\s<([A-Z]+)>\s(\d+\s)*<\/(\1)>$