Skip to content
Advertisement

Why Regular Expression is not working correctly?

I was trying to solve Valid Number problem on leetcode. My choice of language for this problem is Java.

Here is the problem statement if you do not wish to visit website.

Problem Statement

A valid number can be split up into these components (in order):

  1. A decimal number or an integer.

  2. (Optional) An ‘e’ or ‘E’, followed by an integer.

A decimal number can be split up into these components (in order):

  1. (Optional) A sign character (either ‘+’ or ‘-‘).

  2. One of the following formats:

    1. At least one digit, followed by a dot ‘.’.

    2. At least one digit, followed by a dot ‘.’, followed by at least one digit.

    3. A dot ‘.’, followed by at least one digit.

An integer can be split up into these components (in order):

  1. (Optional) A sign character (either ‘+’ or ‘-‘).

  2. At least one digit.

For example, all the following are valid numbers:

["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

while the following are not valid numbers:

["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

Given a string s, return true if s is a valid number.

My Approach

I tried to match the given string against the following regular expression.

((\+|-)?(\d*\.\d+|\d)[eE]?)

I analysed the whole regular expression on RegExr in Javascript. It’s working fine in Javascript when I check it against inputs. I just simply escaped the escape character to make my regular expression work in Java.

Regular Expression in Javascript :- /+|-?(d*.d+|d)[eE]?/g

Below is the whole code :-

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

But it’s giving me wrong answer. Can someone please help ?

I’d like to understand why the Java version of this regular expression isn’t working ?

I could look at the leetcode discuss section to find an answer, but I’d like an explanation on whats exactly wrong with the Java Version of regular expression.

Advertisement

Answer

Since 3e+7 must match but there is nothing after [eE] your regex looks wrong.

The following regex pass all given tests:

String regex = "^(\+|\-)?(\d+\.\d*|\d*\.\d+|\d+)([eE](\+|\-)?\d+)?$";

We can read that as:

  • ^ must match at begining.
  • (\+|\-)? may be there are + or -.
  • ( decimal1 | decimal2 | integer ) that is, digits dot and maybe more digits, maybe digits dot and digits, only digits.
  • ( 10exponent )? maybe exponent where 10exponent is e followed by maybe sign and an integer.
  • $ nothing more.
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement