0

我想通过具有奇数长度的 01 正则表达式字符串进行匹配。例子:"0","001","111","11111" etc.

这个想法是奇数长度的序列是 0 或 1,后跟成对的 0 或 1。但我的正则表达式似乎不起作用。我做的:

String regex = "[0-1]{1}[[0-1]{2}]{0,}";
    String txt = "01";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(txt);

        System.out.println(m.matches());
4

1 回答 1

5

尝试这个:

String regex = "[01]([01][01])*";
"00011".matches(regex) => true
"0001".matches(regex) => false
于 2013-01-16T12:50:53.150 回答