-2

What I want to do is validate user inputs. The criterion is only numeric inputs are allowed, no alpha, no characters like .,/?<> etc.
Say a user inputs 1989, it will print true But if the user inputs anything other than numeric characters, say 1989a or a1989 etc, even one character will suffice for the function to print false, then ask the user for another input.

I tried something like this:

import re

def ask_number() -> None:
    # prompt positive int inputs
    while True:
        card = input("Number: ")

        # non numeric input
        if re.search("![0-9]", card):
            print("false")
            continue
        
        print("true")
        break

ask_number()

but it prints "true" for some reason.


Then I also tried using findall:

# non numeric input
if re.findall("![0-9]", card):
    print("false")
    continue

same result.


Then I tried the caret:

# non numeric input
if re.findall("^![0-9]", card):
    print("false")
    continue

still to no avail. It seems like the bang didn't do anything. I, myself haven't quite mastered regular expressions use in Python. Any help would be appreciated.

11
  • 1
    ! is not a thing for regular expressions. You want [^0-9] instead. Commented Aug 9, 2024 at 3:39
  • The caret is the correct character, but in the wrong place. What documentation did you find it in? Did it have an example? Commented Aug 9, 2024 at 3:40
  • 1
    Also take advantage of predefined classes. In this case, \D already means "anything that isn't a digit" so if a string matches \D, it's not purely numerical. Commented Aug 9, 2024 at 3:48
  • @JohnGordon Ah, that's why it's not working... so in my example, what would "![0-9]" mean? Thank you. Commented Aug 9, 2024 at 3:48
  • @Mike'Pomax'Kamermans True, I think i missed that when reading the documentation. I'm too sleepy to read the long documentation. Appreciate it! Commented Aug 9, 2024 at 3:51

3 Answers 3

0

I missed the special characters when reading the documentation. The easiest way is just use the "\D" as stated in comments. Not only "\D", there are more, such as "\W" and "\S". Please refer to the documentation https://docs.python.org/3/library/re.html


Instead, what I should do is:

# non numeric input
if re.search("\\D", card):
    print("false")
    continue

Or:

# non numeric input
if re.search(r"\D", card):
    print("false")
    continue

Thank you to all who have contributed to answer my question.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the str.isdigit method instead since it returns True only if all characters in a string are digits:

while True:
    card = input("Number: ")
    if card.isdigit():
        break
    print('Please enter a number.')

1 Comment

This can also be one of many ways to do it. I didn't know you could do this, thank you for your answer!
-1
if re.search("[a-z]", card):
            continue

here with search you are looking for any a-z in the input, if yes, continue

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.