Whats wrong with this Regex(JAVA)?


  • 0
    D

    the following code would return "e9" as true. why?

    public class Solution {
        public boolean isNumber(String s) {
            String pattern = "\\s*[+-]?((\\d+.?\\d*)|.\\d+)(e[+-]?\\d+)?\\s*";
            
            return s.matches(pattern);
        }
    }
    

  • 0
    D

    ok i got this. now the correct answer. a little slow though. might need to use other ways;

    public class Solution {
        public boolean isNumber(String s) {
            String pattern = "\\s*[+-]?((\\d+\\.?\\d*)|\\.\\d+)(e[+-]?\\d+)?\\s*";
            
            return s.matches(pattern);
        }
    }
    

Log in to reply
 

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