java正则表达式matches,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.

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

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

}

}

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?

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.

I'm using JDK 6u31.

Any ideas appreciated.

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.

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.

Thanks everyone for their contribution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值