Java regular expression, hopefully this one is more understandable

  • 12
    public class Solution {
    public boolean isNumber(String s) {
    	s = s.trim();
    	if (s.length() == 0)
    		return false;
    	if (s.matches("[+-]?(([0-9]*\\.?[0-9]+)|([0-9]+\\.?[0-9]*))([eE][+-]?[0-9]+)?"))
    		return true;
    		return false;


  • 0

    I think the RegEX should be this "^[+-]?(([0-9]\.?[0-9]+)|([0-9]+\.?[0-9]))([eE][+-]?[0-9]+)?$"
    this mabey more concise!

  • 0

    why \. not .
    and why[0-9] not \d

    the latter 2 will fail tests.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.