Regular expression not outputting value correctly

num = re.findall (r'[-+]?\d*\.*\d+' , str (table))

      

Hi everyone, I have this regex and it prints out the values โ€‹โ€‹I want. However, they are separate.

For example:

['7', '336.82', '-3.89', '-0.05', '7', '351.60', '7', '322.86', '7', '340.71'] 

      

is what it prints

But I want it to print:

['7,336.82', '-3.89', '-0.05', '7,351.60', '7,322.86', '7,340.71']

      

Please can anyone help?

Thanks in advance.

+3


source to share


2 answers


It looks like you want to write down the comma separated numbers. You can use:

r'[-+]?(?:\d+[\d,]*)?\.?\d+'

      



Demo version of RegEx

+3


source


If confirmation of numbers with obligatory 3 digits after the decimal point is required:

[-+]?\d{1,3}(\,\d{3})*(\.\d+)?

      

if the input 1,000,00.0

in this answer means: 1,000

and 00.0

.

Demo: https://regex101.com/r/8nYbaQ/2



Should 01,123

be rejected: (due to start digit 0)

(\+?[1-9]|\-\d)\d{0,2}(\,\d{3})*(\.\d+)?

      

Demo: https://regex101.com/r/8nYbaQ/3

0


source







All Articles