Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex String.matches working inconsistently

Tags:

java

regex

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.

like image 493
Hess Avatar asked Nov 14 '22 07:11

Hess


1 Answers

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.

like image 148
Hess Avatar answered Nov 16 '22 04:11

Hess