简体   繁体   English

Java正则表达式String.matches工作不一致

[英]Java regex String.matches working inconsistently

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. 后十进制部分是可选的。

The issue is that at some point String.matches() function stops working as expected. 问题是在某些时候String.matches()函数停止按预期工作。 What worked before, does not work anymore. 之前有用的东西,不再适用了。

For example, JUnit code: 例如,JUnit代码:

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));
    }
}

The last line with "8 734" fails. “8 734”的最后一行失败。 When I replace it with "1 000" it continues to fail. 当我用“1 000”替换它时,它继续失败。 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!). 最终,相同运行的相同代码在第4行断言中传递,但在最后一次失败(新代码被保存!)。 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()有内存还是什么?

Could there be something wrong with the regular expression? 正则表达式可能有问题吗? I'm skipping ^$ as String.matches works on whole string anyway. 我正在跳过^$作为String.matches无论如何都适用于整个字符串。 I have tried java.util.regex and jregex packages, the issue persisted. 我试过java.util.regex和jregex包,问题仍然存在。

I'm using JDK 6u31. 我正在使用JDK 6u31。

Any ideas appreciated. 任何想法都赞赏。

UPD: ok, after posting this Q the code started to work and hasn't fail so far. UPD:好的,在发布此Q之后,代码开始工作并且到目前为止还没有失败。 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. 到现在为止,我能够通过调试,阅读文档和搜索其他Q的论坛来解决我的问题。

OK, so far I haven't encountered this issue anymore. 好的,到目前为止我还没有遇到过这个问题。

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. 对于偶尔遇到这个问题的其他人,我只能建议清理你正在使用的环境。这必须对损坏的JVM或计算机的内存状态做一些事情。

Thanks everyone for their contribution. 谢谢大家的贡献。

BTW: Try to use pre-compiled pattern Pattern p = Pattern.compile(regEx) and single matcher for all assertions assertTrue(p.matcher("1 000 000").matches()) . 顺便说一句:尝试使用预编译的模式Pattern p = Pattern.compile(regEx)和单个匹配器用于所有断言assertTrue(p.matcher("1 000 000").matches()) String's matches method compiles regex pattern every time you call it String的匹配方法在每次调用时编译正则表达式模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM