javaregexbitset

Regex "\\d+" finds integer twice in string: "{10}". matcher matches 10 and 0


Im using regex to convert BitSet normal toString to a binary string. for instance if the myBitSet.toString() returns {10}, it sets the 10th and the 0th bit, but should only set the 10th.

    ...
    Matcher m = Pattern.compile("(?=(" + "\\d+" + "))").matcher(temp);
    while(m.find()) {
        String test2 = m.group(1);
        answer.setCharAt((2*OpSize -1 - Integer.valueOf(m.group(1))), '1');

    }
    .....

Solution

  • Your problem is that the regex is using a lookahead assertion (?=...) which finds overlapping matches. I can't think of a reason why you would need that in these circumstances.

    Try removing it; that will make sure that only entire numbers are found. You also don't need a capturing group since you can simply use the entire match which is in .group(0) anyway:

    Matcher m = Pattern.compile("\\d+").matcher(temp);
    while(m.find()) {
        String test2 = m.group(0); // why is this here? You're not using it.
        answer.setCharAt((2*OpSize -1 - Integer.valueOf(m.group(0))), '1');
    }