java regex :( C标识符后跟__)

时间:2011-09-06 16:01:00

标签: java regex

我正在寻找一种匹配C标识符后跟双下划线的方法。但事情就是这样:如果标识符以一系列下划线结尾,我需要它是非贪婪的。

我差不多用^([_A-Za-z][_A-Za-z0-9]*?)__得到了它,但是有一套棘手的案例,其中标识符可以以一系列下划线结尾:

string          expected identifier
abcd0__         abcd0
abcd0___        abcd0_
abcd0____       abcd0__
abcd__0__       abcd
abcd___0__      abcd_
abcd____0__     abcd__

有没有办法可以修改正则表达式以产生上面列出的预期组匹配?

下面是一个打印错误输出的测试程序:

abcd0__ -> match is abcd0
abcd0___ -> match is abcd0
abcd0____ -> match is abcd0
abcd__0__ -> match is abcd
abcd___0__ -> match is abcd
abcd____0__ -> match is abcd

Regex3.java:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex3 {
    final static private Pattern pattern = 
       Pattern.compile("^([_A-Za-z][_A-Za-z0-9]*?)__");

    static public void main(String[] args)
    {
        String[] items = {
                "abcd0__",
                "abcd0___",
                "abcd0____",
                "abcd__0__",
                "abcd___0__"
                "abcd____0__"
        };
        for (String item : items)
            test(item);
    }
    private static void test(String item) {
        Matcher m = pattern.matcher(item);
        if (m.find())
        {
            System.out.println(item+" -> match is "+m.group(1));
        }
        else
        {
            System.out.println(item+" -> no match");            
        }
    }
}

2 个答案:

答案 0 :(得分:1)

模式^([_A-Za-z][_A-Za-z0-9]*?_*)_{2}应与您期望的标识符匹配。

答案 1 :(得分:1)

^[_A-Za-z](?:_?[A-Za-z0-9])*_*(?=__)

在squarefree shell中的JavaScript中,

var re = /^[_A-Za-z](?:_?[A-Za-z0-9])*_*(?=__)/
var arr = ["abcd0__", "abcd0___", "abcd0____", "abcd__0__", "abcd___0__",
           "abcd____0__", "abcd", "abcd_"]
for (var i = 0; i < arr.length; i++) {
  print(arr[i] + " : " + re.exec(arr[i]));
}

产生

abcd0__ : abcd0
abcd0___ : abcd0_
abcd0____ : abcd0__
abcd__0__ : abcd
abcd___0__ : abcd_
abcd____0__ : abcd__
abcd : null
abcd_ : null