Java regular expression, hopefully this one is more understandable


  • 12
    J
    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;
    	else
    		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
    H

    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.