Java 3-line regex


  • 1
    S
    public class Solution {
        public boolean isNumber(String s) {
            if (s.trim().length()==0) return false;
            String regexp = "^(\\+|-)?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)(e(\\+|-)?[0-9]+)?$";
            return s.trim().replaceAll(regexp,"").length()==0;
        }
    }
    

Log in to reply
 

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