Java正则表达式模式匹配任何在线测试程序,但不在Eclipse [英] Java Regex pattern that matches in any online tester but doesn't in Eclipse

查看:182
本文介绍了Java正则表达式模式匹配任何在线测试程序,但不在Eclipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一段代码,我不能使它在Eclipse上安装Java 1.7。并从每个匹配中提取2个字符串,所以我使用2个组。



我已经在许多网站(在线正则表达式测试器)中测试了我的表达式,并且它适用于他们破解它不能在Eclipse中的Java项目上工作。



源字符串看起来像以下任何一个:



正式语言:isNatural



注释工具:isHuman%Human Annotator:isHuman



混合注释:概念类型混合注释工具:概念类型混合标签集:概念类型



...和



我想提取:之前的第一个字,每次匹配之后的单词。



我正在使用的正则表达式是:



(\w * \s * \w +):( \s + \w +)% {0,1}



和剪辑宠物的代码:

  String attribute = parts [0]; 
模式模式= Pattern.compile((\\w * \\\ss \\ \\\w +):( \\s + \\w +)%{0,1} );
Matcher matcher = pattern.matcher(attribute);
OWLDataProperty dataProp = null;

if(matcher.matches()){
while(matcher.find()){
String name = null,domain = null;

domain = matcher.group(1);
name = matcher.group(2);

dataProp = factory.getOWLDataProperty(:+ Introspector.decapitalize(name),pm);

OWLClass domainClass = factory.getOWLClass(:+ domain.replaceAll(,),pm);
OWLDataPropertyDomainAxiom domainAxiom = factory.getOWLDataPropertyDomainAxiom(dataProp,domainClass);
manager.applyChange(new AddAxiom(本体,domainAxiom));
}

有人知道为什么不起作用吗?



非常感谢。

解决方案

使用 matches() code>,您询问您提供的字符串是否与您的正则表达式整体匹配。就好像您在正则表达式的开头添加了 ^ ,最后添加了 $



您的正则表达式否则很好,并返回您期望的内容。我建议您测试它 regexplanet.com,Java模式。当 matches()为true时,您会看到 false ,以及每个查找()将返回。



为了解决你的问题,我想你只需要删除 if(matcher。 matches())条件。


I have a piece of code that I can't make it working on Eclipse with Java 1.7 installed.

There is a regex expression I want to use to match and extract 2 strings from every match, so I am using 2 groups for this.

I have tested my expression in many websites (online regex testers) and it works for them bust it isn't working on my Java project in Eclipse.

The source string looks like anyone of these:

Formal Language: isNatural

Annotation Tool: isHuman%Human Annotator: isHuman

Hybrid Annotation: conceptType%Hybrid Annotation Tool: conceptType%Hybrid Tagset: conceptType

... and so on.

I want to extract the first words before the ":" and the word after for every match.

The regex I'm using is this:

(\w*\s*\w+):(\s+\w+)%{0,1}

And the snippet of code:

String attribute = parts[0];                            
Pattern pattern = Pattern.compile("(\\w*\\s*\\w+):(\\s+\\w+)%{0,1}");
Matcher matcher = pattern.matcher(attribute);
OWLDataProperty dataProp = null; 

if (matcher.matches()){
    while (matcher.find()){
        String name = null, domain = null;

        domain = matcher.group(1);
        name = matcher.group(2);

        dataProp = factory.getOWLDataProperty(":"+Introspector.decapitalize(name), pm); 

        OWLClass domainClass = factory.getOWLClass(":"+domain.replaceAll(" ", ""), pm);
        OWLDataPropertyDomainAxiom domainAxiom = factory.getOWLDataPropertyDomainAxiom(dataProp, domainClass);
        manager.applyChange(new AddAxiom(ontology, domainAxiom));
    }

Does anybody of you know why it's not working?

Many thanks.

解决方案

When using matches(), you are asking if the string you provided matches your regex as a whole. It is as if you added ^ at the beginning of your regex and $ at the end.

Your regex is otherwise fine, and returns what you expect. I recommend testing it regexplanet.com, Java mode. You will see when matches() is true, when it false, and what each find() will return.

To solve your problem, I think you only need to remove the if (matcher.matches()) condition.

这篇关于Java正则表达式模式匹配任何在线测试程序,但不在Eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆