I've got a string coming into the function, and all I need to do here is return true or false depending on the contents.
If you can't tell by my regex, the string should look something like
'2' or '2+ 2' or '4 +2 * 0'
and should not look anything like
'asldkfjasldkfjaslkdfj' or '2 - 3' or '9 / 12'
yet somehow, when those last three strings are passed in, my function is still returning true. What's wrong here?
>>265994
Think about what you're doing with the repetition operator (*). It means "ZERO or MORE". You're basically matching when there are ZERO instances of what you're looking for.
>>265994
Also "\s" is just a stupid way of writing "."
>>265994
Also
if (condition):
__return(true)
return(false)
is an antipattern in any language, not just Python.
Just write
return (condition)
>>266014
\s matches whitespace. The dot matches anything.
>>266008
how can i see if the entire string matches the conditions instead of if a match exists in it
>>266026
It's already doing that, it's just that your regex matches a bunch more whole strings than you intend to.
>>266032
can you tell me how it's matching strings like "asdasdfhasdf" and "2/3"? I've been using an online tool to check and it doesnt work on the tool
>>266043
You need to nail it down with ^ and $.
Go work through regexone.com .
>>266026
You don't understand the repetition operator. It matches ZERO OR MORE. Think about this:
Suppose you're trying to match the letter A and you do:
A*
Now think about what this does. It matches A zero or more times. So it will match in the following instances:
A
AA
B ( it matches once because it found the letter A zero times)
Now you understand what your problem is? Turn the repitition operator from * (zero or more times) to + (one or more times)
>>266068
Use this page to test your regular expressions: http://regexpal.com.s3-website-us-east-1.amazonaws.com/
Now you can do this. It will match what you're looking for. Like >>266064 said, you might want to use ^ and $ to match the string exactly.