I have a file with just 3500 lines like these:
filecontent= "13P397;Fotostuff;t;IBM;IBM lalala 123|IBM lalala 1234;28.000 things;;IBMlalala123|IBMlalala1234"
Then I want to grab every line from the filecontent
that matches a certain string (with python 2.7):
this_item= "IBMlalala123"
matchingitems = re.findall(".*?;.*?;.*?;.*?;.*?;.*?;.*?"+this_item,filecontent)
It needs 17 seconds for each findall
. I need to search 4000 times in these 3500 lines. It takes forever. Any idea how to speed it up?
str.split
on the large string, and run the regexp on the small individual strings. \$\endgroup\$?
in.*?
). The regexes are easier to match if they can simply match whatever they want, without thinking of finding the minimum match that works. In some situations finding a "greedy pattern" becomes really complex, in which case it depends on the speed you need to have as a requirement and the readability you want to achieve. \$\endgroup\$