helloB helloB - 1 month ago 9
Python Question

Extract all characters between _ and .csv

I am trying to extract the date from a series of files of the form:

costs_per_day_100516.csv


I have gotten to the point of extracting the
6
, but I don't understand why I can't extract more. What is wrong with the following:

test_string = 'search_adwords_cost_by_state_100516.csv'
m = re.search("_([^_])*\.csv", test_string)
m.group(1)


This yields
6
rather than
100516
. What am I doing wrong?

Answer
m = re.search("_([^_]*)\.csv", test_string)

The repetition qualifier has to be inside the capture