I am making a simple project for my math class in which I want to verify if a given function body (string) only contains the allowed expressions (digits, basic trigonometry, +, -, *, /).

I am using regular expressions with the `re.findall`

method.

My current code:

```
import re
def valid_expression(exp) -> bool:
# remove white spaces
exp = exp.replace(" ", "")
# characters to search for
chars = r"(cos)|(sin)|(tan)|[d+/*x)(-]"
z = re.findall(chars, exp)
return "".join(z) == exp
```

However, when I test this any expression the `re.findall(chars, exp)`

will return a list of tuples with 3 empty strings: `('', '', '')`

for every character in the string unless there is a trig function in which case it will return a tuple with the trig function and two empty strings.

Ex: cos(x) -> `[('cos', '', ''), ('', '', ''), ('', '', ''), ('', '', '')]`

I don’t understand why it does this, I have tested the regular expression on regexr.com and it works fine. I get that it uses javascript but normally there should be no difference right ?

Thank you for any explanation and/or fix.

1

What’s the value of

`x`

? Your`[...]`

isn’t doing what you probably think it is.`d+`

is two separate characters to include in the bracket expression, not a regular expression for multidigit integers.