Create new line based on each regex match in python
Question: I have an input file that contains data formatted as follows:

a; b, c| derp derp "X1234567, Y1234567, Z1234567" derp derp a; b, c|

I would like to use Python to parse that into multiple lines for each item
that occurs between double quotes.

The output for the above example would be:

a; b, c| derp derp X1234567 derp derp a; b, c|

a; b, c| derp derp Y1234567 derp derp a; b, c|

a; b, c| derp derp Z1234567 derp derp a; b, c|

So far I have something like this:

    
    
    import re
    prefix = re.compile ('^(.*?)"')
    pattern = re.compile('\"(.*?)([A-Z]{1}[0-9]{7})(.*?)\"')
    suffix = re.compile ('"(.*?)$')
    for i, line in enumerate(open('myFile.txt')):
        for match in re.finditer(pattern, line):
            print prefix, match.group(), suffix
    

But it only seems to return the first match of each of the contents.


Answer: In this situation it's actually alot more work (in my opinion) to use regex
rather than simple string and list manipulations. I would just do it as such:

    
    
    #!/usr/bin/env pytohn
    
    with open('myFile.txt','r') as f:
        lines = readlines(f)
    
    for line in lines:
        line = line.strip()
        start = line.find('"')
        end = line.find('"',start+1)
        data = line[start+1:end].split(',')
        data = [x.strip() for x in data]
        for x in data:
            print line[:start],x,line[end+1:]
    

Here's what I found after taking a look at the code you posted:

  * You're actually printing `SRE_Pattern` objects for `prefix` and `suffix` in the print line. You should record matches for prefix and suffix on every iteration of the outer for loop.
  * Calling `match.group()` will return the entire match, not just what's in parentheses. I think you want `match.group(1)` in most cases.
  * Having `pattern` defined like that only matches one string because it searches sequentially through the lines for a starting quotation mark followed by the rest of the pattern. Therefore it gets to the index for the first quotation mark, checks once for a pattern, finds `X1234567` then moves on.
  * I'm not sure why you have backslashes before the quotation marks in `pattern`, as I don't think those are special characters.
  * In your `suffix`, it will match the first quotation mark not the second, and thus `suffix` will include the stuff between the quotation marks.
  * The print statement will insert spaces between items if you use commas, so you should concatenate them using `+` instead.

And finally here is what I ended up with for regex:

    
    
    #!/usr/bin/env python
    
    import re
    
    prefix = re.compile('^(.*?)"')
    quotes = re.compile('".*?(.*).*?"')
    pattern = re.compile('[A-Z]{1}[0-9]{7}')
    suffix = re.compile('".*"(.*?)$')
    
    for (i,line) in enumerate(open('myfile.txt')):
        pre = prefix.search(line).group(1)
        data = quotes.search(line).group(1)
        suf = suffix.search(line).group(1)
        for match in re.finditer(pattern,data):
            print pre+match.group(0)+suf
    

Hope this helps, any questions please ask. Regex is a tricky beast at the best
of times.

