My simple 3 regex Java solution.


  • 1
    B
    public boolean isNumber(String s) {
    	s = s.trim();
    	boolean isNumber = false;
    	
    	if(s.matches("(-?|\\+?)((\\d+)?\\.\\d+|\\d+\\.(\\d+)?)")) {
    		isNumber = true;
    	}
    	if(s.matches("(-?|\\+?)(\\d+\\.(\\d+)?|(\\d+)?\\.\\d+|\\d+)[e](-?|\\+?)\\d+")) {
    		isNumber = true;
    	}
    	if(s.matches("(-?|\\+?)\\d+")) {
    		isNumber = true;
    	}
    		
    	return isNumber;
    }
    

    First regex to test for decimal, second for scientific notation, third for regular numbers.


  • 1

    great answer.


Log in to reply
 

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