Java正则表达式String.matches工作不一致 [英] Java regex String.matches working inconsistently

查看:134
本文介绍了Java正则表达式String.matches工作不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式,用于检查字符串是否为数字。格式的千位分隔符是空格,小数点分隔符是点。后十进制部分是可选的。

I have a regex which checks if a string is a number. The format's thousand separator is a white space, decimal separator is a dot. After-decimal part is optional.

问题是在某些时候String.matches()函数停止按预期工作。以前有效的方法,不再有效。

The issue is that at some point String.matches() function stops working as expected. What worked before, does not work anymore.

例如,JUnit代码:

For example, JUnit code:

import junit.framework.Assert;
import org.junit.Test;

public class RegExTest {

    @Test
    public void testThousandSeperatorRegex()
    {
        String regEx = "([0-9]{1,3}( [0-9]{3})*(\\.[0-9]+)?|\\.[0-9]+)";
        Assert.assertEquals(true, "1".matches(regEx));
        Assert.assertEquals(true, "10".matches(regEx));
        Assert.assertEquals(true, "100".matches(regEx));
        Assert.assertEquals(true, "1 000".matches(regEx));
        Assert.assertEquals(true, "10 000".matches(regEx));
        Assert.assertEquals(true, "100 000".matches(regEx));
        Assert.assertEquals(true, "1 000 000".matches(regEx));
        Assert.assertEquals(true, "10 000 000".matches(regEx));
        Assert.assertEquals(false, "10000.56".matches(regEx));
        Assert.assertEquals(true, "8 734".matches(regEx));
    }
}

8 734的最后一行失败。当我用1 000替换它时它继续失败。最终,相同运行的相同代码在第4行断言中传递,但在最后一次失败(新代码被保存!)。
但有时候一切都开始按预期工作,直到......开始再次失败。所以我想我很难重现我的问题。也许还有其他一些我做错了,我没有注意到并因此而描述,但我试图让它尽可能简单。
这个让我很困惑。 String.matches()有内存还是什么?

The last line with "8 734" fails. When I replace it with "1 000" it continues to fail. Eventually, the same code at the same run passes in the 4th line of assertions, but fails in the last (the new code is saved!). But there are times when everything starts working just as expected until.. start failing again. So I suppose that it will be hard to reproduce my issue. Maybe there are something else that I'm doing wrong which I haven't noticed and thus described, but I tried to make it as plain as possible. This one confuses me a lot. Does String.matches() has a memory or what?

正则表达式有问题吗?我正在跳过 ^ $ ,因为String.matches无论如何都适用于整个字符串。我已经尝试过java.util.regex和jregex包,问题仍然存在。

Could there be something wrong with the regular expression? I'm skipping ^$ as String.matches works on whole string anyway. I have tried java.util.regex and jregex packages, the issue persisted.

我正在使用JDK 6u31。

I'm using JDK 6u31.

任何想法都赞赏。

UPD:好的,发布此Q后代码开始工作并且到目前为止还没有失败。也许这对我有用,但自从上周以来我一直困扰着我,并且我能够一次又一次地复制它。我将继续我的代码,如果它将继续工作,我将关闭此问题。此外,我将尝试确定究竟是什么原因造成的问题。
同时,如果有人遇到同样的问题,请分享您的知识。否则,这看起来像是一个可以通过知识而不是通过调试解决的问题。
为了保护自己免于愚蠢我可以说我已经编程多年了,这是论坛上的第一篇文章:)。到现在为止,我能够通过调试,阅读文档和搜索其他Q的论坛来解决我的问题。

UPD: ok, after posting this Q the code started to work and hasn't fail so far. Maybe it was something with me, but this has bothered me since last week and I have been able to replicate it again and again. I will continue with my piece of code and if it will continue to work I will close this issue. Also I will try to determine what exactly caused the problem. Meanwhile, if there are someone out there who has encountered the same issue, please share your knowledge. Otherwise, this looks like an issue that can be solved by knowledge, not by debugging. To defend myself from stupidity I can say I have been programming for many years and this is the 1st ever post in forums :). Until now I was able to solve my problems with debugging, reading docs and searching forums of other Qs.

推荐答案

好的,到目前为止我还没有遇到过这个问题。

OK, so far I haven't encountered this issue anymore.

对于偶尔遇到这个问题的其他人,我只能建议清理你正在使用的环境。这必须对损坏的JVM或计算机做些什么。记忆状态。

For other who happen to meet this one someday, I can only suggest to clean up the environment that you are working in. This has to do something with corrupted JVM or computer's memory state.

感谢大家的贡献。

这篇关于Java正则表达式String.matches工作不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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