Ethan
Ethan

Reputation: 155

Java RegEx - for an Integer not containing a "."

I need to be able to return signed and unsigned integer constants with no intervening symbols, possibly preceded by + or -. The only allowed digits are 3, 4, and 5.

I can't figure out a way to say that the expression must not contain a period before or after the integer.

This is what I have so far, but if I pass say "34.5 - 43" the string returned will be: "34 5 43".

All that needs to be returned is "43".

public String getInts(String toBeScanned){

    String INT = "";
    Pattern p = Pattern.compile("\\b[+-]?[3-5]+\\b");
    Matcher m = p.matcher(toBeScanned);

    if (m.matches() == true){
        INT = toBeScanned;
    }
    else{
        m = p.matcher(" " + toBeScanned);
        while (m.find()){
        INT = INT + m.group() + " ";
        }
    }
    return INT;
}

Any thoughts or pushes in the right direction are appreciated. Is there a way to say it that the first and last character can be [\b and not .]

This is frustrating the heck out of me. Help!

Upvotes: 1

Views: 620

Answers (3)

stema
stema

Reputation: 92986

You don't want a word boundary \b here. I think the best is to create your own assertion, try this

(?<![.\d])[+-]?[3-5]+(?![.\d])

See it here on Regexr

(?<![.\d]) is a negative lookbehind assertion, it says before the pattern is no dot and no digit allowed.

(?![.\d]) is a negative lookahead assertion, it says after the pattern is no dot and no digit allowed.

Improvement

to avoid that it matches stuff like "hf34" we can make it more strict

(?<![.\w])[+-]?[3-5]+(?![.\w])

See it on Regexr

The word boundary \b

\b matches on a change from a word character to a non word character. A word character is a letter or a digit or a _. That means you will also get problems with your \b before the [+-], because there is no \b between a space/start of the string and a [+-].

Upvotes: 2

alex
alex

Reputation: 1229

Is there a way to say it that the first and last character can be [\b and not .]

[^\.\b]

matches \b but not '.'

Is that what you are looking for?

[^\.\b][+-]?[3-5]+[^\.\b]

Will match '43' but not '34.5'

Upvotes: 0

gorjusborg
gorjusborg

Reputation: 656

"\b[+-]?[3-5]+[.][3-5]+\b"

This pattern says that in order to match, there must be at least one number before, and one number after the decimal point.

Upvotes: 0

Related Questions