1

I'm using [+-]?[0-9]*.[0-9]{1,} this simple regex for detecting simple floating numbers. It's working fine but it's detecting 4.0O0 as a floating point number. Can anyone explain me why ? i'm doing it using python , the code is given below:

    pattern=r'[+-]?[0-9]*\.[0-9]{1,}'
    input_float=input().strip()
    if re.match(pattern,input_float):
         print("True")
    else:
         print("False")

1 Answers1

1

re.match only matches the given regex at the beginning of the string, but not the end of the string. Use re.fullmatch instead to match the entire string with the given regex.

if re.fullmatch(pattern,input_float):
blhsing
  • 91,368
  • 6
  • 71
  • 106