1

我希望从完全限定的类名中清除除类名之外的所有内容。所以,我可能有类似的东西......

"class gqlMain.Node"

......我想结束......

"Node"

...我很确定我的模式...

"*.[\\.][^\\.]*"

..是正确的,如果只是像上面那样运行它并使用...进行测试

myMatcherObject.matches()

...它总是返回 true,但是当我尝试添加分组时,比如...

"(.*[\\.])([^\\.]*)"

...我总是得到找不到匹配的错误。不知道发生了什么。

添加:

感谢您的快速回复,伙计们。是的,我真的不明白这个。我的确切代码是....

public String toString() {
    Pattern packagePatt = Pattern.compile("(.*[\\.])([^\\.]*)");
    // 
    System.out.println(this.compClass.getName().toString());

    Matcher packageMatch = packagePatt.matcher(this.compClass.getName().toString());

    //
    System.out.println(packageMatch.group(2));
    return packageMatch.group(2);
}

例如,第一个打印语句会生成一个类似“gqlMain.Node”的字符串(我知道 toString() 是多余的,我出于恼怒添加了它)。第二个 print 语句会产生错误,return 语句也会产生错误。使用调试器,我可以看到 Matcher 对象的组列表在每个索引处都为空。但是如果我插入一个...

if (packageMatcher.matches()) {
    // print true
}

...我总是得到“真实”。这真的没有意义。

4

2 回答 2

1

以下程序报告“真实”:

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




public class so {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Pattern p = Pattern.compile("(.*[\\.])([^\\.]*)");
        Matcher m = p.matcher("class gqlMain.Node");
        System.out.println(m.matches());
        System.out.println(m.group(0));
        System.out.println(m.group(1));
        System.out.println(m.group(2));

    }

}

完整的输出是:

true
class gqlMain.Node
class gqlMain.
Node
于 2012-07-19T05:23:37.263 回答
0

I wouldn't recommend to scan for the identifiers in that way (but I believe you wanted not to over-engineer), and you probably will like the following solution that is more strict for scanning the identifiers in general (however, speaking frankly, I don't believe I'm scanning for an identifier in the most correct way too). Additionally, it can scan for several fully/partially qualified identifiers within a single string, but it completely ignores non-qualified one (e.g. class is ambiguous).

package stackoverflow;

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

import static java.lang.System.out;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.compile;

public final class Q11554180 {

    private Q11554180() {
    }

    //
    // (3) The same as item (1) however we're       ------------------------------------------------+
    //     capturing the group to get the class                                                     |
    //     name only                                                                                |
    // (2) At least one package name is required    ---------------------------------+              |
    // (1) We're searching valid package names only -----------------+               |              |
    //     and we do not need to capture it ?:                       |               |              |
    //                                              +----------------+--------------+|+-------------+-------------+
    //                                              |                               |||                           |
    private static final Pattern pattern = compile("(?:[\\p{Alpha}_][\\p{Alnum}_]*\\.)+([\\p{Alpha}_][\\p{Alnum}_]*)", CASE_INSENSITIVE);

    private static void find(CharSequence s) {
        final Matcher matcher = pattern.matcher(s);
        while ( matcher.find() ) {
            out.println(matcher.group(1));
        }
    }

    public static void main(String[] args) {
        find("class gqlMain.Node; class gqlMain.p1.NodeA");
        find("class gqlMain.p1.p11.NodeB");
        find("class gqlMain.p1.p11.p111.NodeC");
        find(Q11554180.class.getCanonicalName());
    }

}

The code above will produce the following output:

Node
NodeA
NodeB
NodeC
Q11554180
于 2012-07-21T22:24:53.550 回答