Get matching lines from a text file

I have the following text file and I want to get the numbers in brackets

ID&number:Track_number(12930)_ ID&number:Track_number(394839)_ ID&number:Track_number(958236)_ 

So i tried this

  import re file = open("text.txt", "r") text = file.read() file.close() pattern = re.compile(ur'Track_number(.*)_', re.UNICODE) string = pattern.search(text).group(1) print string 

But it only displays the first result: (12930) . I was wondering if it is possible to get a list of all the relevant results. Thanks

+4
source share
2 answers

You can use re.findall for example

 >>> re.findall('\((\d+)\)', text) ['12930', '394839', '958236'] 
+2
source

All you have to do is replace search with findall . This will result in a list all matches.

+1
source

All Articles