python – Regex expression to ignore digits, commas, periods and a “-” if it is the first thing in the expression

I have a function that needs to receive strings and try to cast them as either int or float.

To do that first I detect if the string isn’t empty and then I need to check for letters and all possible special characters. I’ve been trying to do this by making a regex expression that ignores all digits, commas and periods as well as a single “-” if it is the first character of a string.

So far I got everything except the “-” at the beggining with “[^.,\d]”.

Everything I’ve done to include the “-” detection ends up messing with either the digit, comma or period ignore.

For clarification on the detection of “-“:

-9   valid
-9-  invalid
9-   invalid

Edit: I have been asked to provide my current regex and the test cases I am using. Here they are:

Regex: [^\d,.]
Valid cases:

-

Invalid cases:

-

Read more here: Source link