regex - Python splitting string to pieces then adjusting their results


Keywords:python 


Question: 

I am trying to make the code below work as intended. I got it to work to accept numbers, - sign for negative numbers and a dot sign for percision. But the code still allows for multiple - and . signs. And it doesn't check if it starts or ends with them.

I want to only get numbers, - sign if its a negative value and only one dot if its a float.

here is my code:

# org_text = gets something from clipboard

new_text = [x.strip() for x in org_text.split(',')]  # as you can see new_text is a list of strings

if len(new_text) > 1:
    text = re.compile(r'[^\d.-]+').sub('', new_text[0])

Later on I use the following for some adjustments:

text = text.lstrip(' .').rstrip('-. ')
text = text[0] + text[1:].replace('-', '')

It doesnt do exactly what I want plus I believe there should be an easier way. I tried playing arround with the regex part but didnt do the trick either.

Any help will be appreciated.

Edit: I didn't make some parts clear. I use the re.compile and sub which you can see from the code. I am trying to check the clipboard text which come in org_text. Then I am seperating the values inside with the comma sign. I am only accepting the regex r'[^\d.-]+' chars and deleting the rest in that original string using ''. At this point I get everything working properly except what I mentioned in my original question. I can get more than one - and . signs AND I also get them in unproper places. That's the part I am trying to fix.


1 Answer: 

Your regex [^\d.-]+ is incorrect. You are trying to negate presence of digits, . and - i.e anything other than these will match.

Use this regex instead.

Regex: ^-?\d+(?:\.\d+)?$

Explanation:

  • -? ensures that negative sign is optional. Will match if present.

  • \d+ matches digits before decimal.

  • (?:\.\d+)? optional decimal part. Will match if present.

Regex101 Demo