python - regex number of repetitions from code -


can use values script inform regexs dynamically how operate?

for example:

base_pattern = r'\s*(([\d.\w]+)[ \h]+)' n_rep = random.randint(1, 9) new_pattern = base_pattern + '{n_rep}' line_matches = re.findall(new_pattern, some_text) 

i keep getting problems trying grouping work


explanation

i attempting find common number of repetitions of regex pattern in text file in order find table type data within files. have idea make regex such this:

base_pattern = r'\s*(([\d.\w]+)[ \h]+)' line_matches = np.array([re.findallbase_pattern, line) line_num, line in enumerate(some_text.split("\n"))])  # find text has similar number of words/data in each line where_same_pattern= np.where(np.diff([len(x) x in line_matches])==0) line_matches_where_same = line_matches[where_same_pattern]  # extract out lines have data interesting_lines = np.array([x x in line_matches_where_same if x != []])  # find how many words in each line of interest len_of_lines = [len(l) l in interesting_lines]  # use prevalent number of columns of data n_cols = counter(len_of_lines).most_common()[0][0]   # rerun data through regex find columns new_pattern = base_pattern + '{n_cols}' line_matches = np.array([re.findall(new_pattern, line) line_num, line in enumerate(some_text.split("\n"))]) 

you need use value of variable, not string literal name of variable, e.g.:

new_pattern = base_pattern + '{' + n_cols + '}' 

Comments