I am trying to replicate certain lines in a file and strip certain substrings from certain replicas. Each line is a string which I get through a fileobject.readline()
Line1 : a -> b,c;
Line2 : d -> e,f,g;
Line3 : h -> i,j;
Line4 : k -> l;
Line1 : a -> b;
Line2 : a -> c;
Line3 : d -> e;
Line4 : d -> f;
Line5 : d -> g;
Line6 : h -> i;
Line7 : h -> j;
Line8 : k -> l;
As far as I understand your question, you can simply replace ',' characters with the '\w ' pattern. It's more like a trick than a solution.
I wrote a simple function that can 'replicate' a line into multiple line follows your requirement. Note that this is for 1 line only.
def replicateALine(line): regex = re.search('\w -> ', line) if regex: line= line.replace(',', ';\n' + regex.group(0)) return line print replicateALine('a -> b,c,d;') print replicateALine('e -> b;')
a -> b; a -> c; a -> d; e -> b;
Update: Because \w will only cover 1 character, if you need to cover things like snu_1opbb_1, try '\w+'