Python Regex - Capturing a sentence based on the beginning and ending
Question: I'm fairly new to python, but I'm attempting to write a program that will
capture a sentence out of a string, based of the beginning and ending of the
sentence.

For example if my string was

    
    
    description = "11:26:16 ENTRY 'Insert Imaginative Description of a person' 11:29:17 EXIT 'Insert The Description of the Same Person'"
    

I know how to do the regex to detect the date stamp and the word entry. I'd
use:

    
    
    re.search(r'\d{2}:\d{2}:\d{2} ENTRY', description)
    

Which would of course tell me that there was one entry at that position, but
how would I make the regex capture the date stamp, entry and the following
sentence, but leave out the EXIT log?


Answer: You may try this.

    
    
    re.search(r'\b(\d{2}:\d{2}:\d{2}(?:\.\d{3})?) ENTRY', description)
    

Use `re.findall` if you want to do a global match since `re.search` would
return only the first match.

**Example:**

    
    
    >>> import re
    >>> description = "11:26:16 ENTRY 'Insert Imaginative Description of a person' 11:29:17 EXIT 'Insert The Description of the Same Person'"
    >>> re.search(r'\b(\d{2}:\d{2}:\d{2}(?:\.\d{3})?) ENTRY', description).group(1)
    '11:26:16'
    

To get also the log after the `ENTRY`.

    
    
    >>> re.search(r"\b(\d{2}:\d{2}:\d{2}(?:\.\d{3})?) ENTRY '([^']*)'", description).group(1)
    '11:26:16'
    >>> re.search(r"\b(\d{2}:\d{2}:\d{2}(?:\.\d{3})?) ENTRY '([^']*)'", description).group(2)
    'Insert Imaginative Description of a person'
    >>> re.search(r"\b(\d{2}:\d{2}:\d{2}(?:\.\d{3})?) ENTRY '([^']*)'", description).group()
    "11:26:16 ENTRY 'Insert Imaginative Description of a person'"
    

