简体   繁体   中英

Java regular expression to match integers from user input

I'm trying to improve my regex skills, so I have made a basic calculator to practice pattern matching. The user is prompted to enter two integer values into the console, separated by a comma, with no spaces. I'm not worried about the values being too large for int to handle, I just want to cover the case of user entering -0. Positive 0, and all other negative and positive values should be accepted.

A scanner object grabs the input from the user and stores it in a string variable. This variable is then passed to a method with a Pattern and Matcher that does the matching and returns a boolean of whether it matched or not.

String userInput = scanner.next();

//look for only integers, especially excluding -0
if(checkUserMathInputIsGood("REGEX", userInput))
    {
        int sum;
        String[] twoNumbersToAdd = userInput.split(",");

        sum = Integer.parseInt(twoNumbersToAdd[0]) + Integer.parseInt(twoNumbersToAdd[1]);

        System.out.println(sum);
    }

After hours of scouring stackoverflow, javadocs, etc., I've found some solutions which almost work.

http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#regex_negative

http://www.regexplanet.com/advanced/java/index.html

Java regular expression for negative numbers?

The pattern example which begins with "T(blah blah)" didn't work at all, and I can't find a reference to what T is supposed to accomplish. I've come close with:

"-{0,1}(?!0)\\d+,-{0,1}(?!0)\\d+"

Breaking it down, this seems to say: allow a minus sign a minimum of 0 and maximum of 1 times. Do not allow 0 if the so-called "negative lookahead" for the minus sign is true. Then allow any integer value at least one integer long. However, this results in the regex rejecting 0 as well as -0.

Examples of input which should be accepted:
2,3
22,-4
-555,-9
0,88
0,0

Examples of input which should be rejected:
-0,9
432,-0
-0,-0

Any help or suggestions is greatly appreciated.

如果我正确理解了要求,那么它应该是"-?\\\\d+,-?\\\\d+"

^(?:(?:\+?\d+)|(?:-(?!0*,)\d+)),(?:(?:\+?\d+)|(?:-(?!0*$)\d+))$

Demo.

Explanation:

^// match start of line or text
(?:// match either:
    (?:// option 1:
        \+? // a "+" if possible
        \d+ // and any number of digits
    )
    |// or
    (?:// option 2:
        - // a "-" sign
        (?!//negative lookahead assertion: do NOT match if next is...
            0*,//...any number of zeroes and a comma
        )
        \d+//if we've made it this far, then we know this integer is NOT zero. Match any number of digits.
    )
)
,// a comma.
(?:// this pattern is basically the same as the one for the first number.
    (?:
        \+?
        \d+
    )
    |
    (?:
        -
        (?!
            0*$// except this matches the end of a line or text instead of a comma.
        )
        \d+
    )
)
$// end of line or text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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