Using regex to parse kindle "My Clippings.txt" file
Question: I am currently trying to use python to parse the notes file for my kindle so
that I can keep them more organized than the chronologically ordered list that
the kindle automatically saves notes in. Unfortunately, I'm having trouble
using regex to parse the file. Here's my code so far:

    
    
    import re
    
    
    def parse_file(in_file):
        read_file = open(in_file, 'r')
        file_lines = read_file.readlines()
        read_file.close()
        raw_note = "".join(file_lines)
    
        # Regex parts
        title_regex = "(.+)"
        title_author_regex = "(.+) \((.+)\)"
    
        loc_norange_regex = "(.+) (Location|on Page) ([0-9]+)"
        loc_range_regex = "(.+) (Location|on Page) ([0-9]+)-([0-9]+)"
    
        date_regex = "([a-zA-Z]+), ([a-zA-Z]+) ([0-9]+), ([0-9]+)"  # Date
        time_regex = "([0-9]+):([0-9]+) (AM|PM)"  # Time
    
        content_regex = "(.*)"
        footer_regex = "=+"
    
        nl_re = "\r*\n"
    
        # No author
        regex_noauthor_str =\
        title_regex + nl_re +\
        "- Your " + loc_range_regex + " | Added on " +\
        date_regex + ", " + time_regex + nl_re +\
        content_regex + nl_re +\
        footer_regex
    
        regex_noauthor = re.compile(regex_noauthor_str)
        print regex_noauthor.findall(raw_note)
    
    parse_file("testnotes")
    

Here is the contents of "testnotes":

    
    
    Title
    - Your Highlight Location 3360-3362 | Added on Wednesday, March 21, 2012, 12:16 AM
    
    Note content goes here
    ==========
    

What I want:

    
    
    [('Title', 'Highlight', 'Location', '3360', '3362', 'Wednesday', 'March', '21', '2012', '12', '16', 'AM', 
    

But when I run the program, I get:

    
    
    [('Title', 'Highlight', 'Location', '3360', '3362', '', '', '', '', '', '', '', '')]
    

I'm fairly new to regex, but I feel like this should be fairly
straightforward.


Answer: You need to escape the `|` in `"- Your " + loc_range_regex + " | Added on " +\`

to: `"- Your " + loc_range_regex + " \| Added on " +\`

`|` is the OR operator in a regex.

