简体   繁体   中英

find specific patern in text using python regular expression

my string is

'21,300 32,709 30,391 29,901 22,270 31,201 31,199 27,806 23,210 28,418 28,940 32,496 16.9%'

how can I find all number but 16.9%??

I used

pattern = re.compile(r'\d+,*\d*(?!%)')

but It was not work.

thanks!

You can use re.findall with those pattern examples:

import re

a = '21,300 32,709 30,391 29,901 22,270 31,201 31,199 27,806 23,210 28,418 28,940 32,496 16.9%'

final = re.findall(r'(\d+)[,\s]', a)
# Or:
# final = re.findall(r'(\d+)[^\d\.%]', a)

Both will output:

print(final)

['21', '300', '32', '709', '30', '391', '29', '901', '22', '270', '31', '201', '31', '199', '27', '806', '23', '210', '28', '418', '28', '940', '32', '496']
import re

a = '21,300 32,709 30,391 29,901 22,270 31,201 31,199 27,806 23,210 28,418 28,940 32,496 16.9%'

re.findall(r'\d+.\d+%', a)

This regex will catch the percentage you want. Basically, i'm catching everything with '%' with one or more digits, separated by dot '.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM