News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Capture the numbers in this regex

posted on 2024-12-02 22:07     read(775)     comment(0)     like(8)     collect(1)


I have a string like this:

{{1090872, "A"}, {4281, "AA"}, {1332552, "AAACU"}, {1287145, "AABB"}}

How can I write a regex to capture the numbers. I know that I can capture the letters with: "(.*?)"


solution


If you don't have number in quotes, then answer is

import re
str = '{{1090872, "A"}, {4281, "AA"}, {1332552, "AAACU"}, {1287145, "AABB"}}'
re.findall(r'\d+', str)
['1090872', '4281', '1332552', '1287145']

otherwise you can try

re.findall(r'[{},](\d+)[{},]', str)


Category of website: technical article > Q&A

Author:qs

link:http://www.pythonblackhole.com/blog/article/247235/cdef14a5d28debffdfcc/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

8 0
collect article
collected

Comment content: (supports up to 255 characters)