Regular expression to find last colon after timestamp

Viewed 300

I have a text string of the following format:

[0:00:02] name1: Okay, this is the continued string...

I would like to get a python regular expression that extracts all the text starting with Okay...

I have figured out how to extract the time stamp and speaker name:

 time_frame = re.search('\[(.*?)\]', temp).group(1)
 speaker_id = re.search('\] (.*?)\:', temp).group(1)

However, am having no luck with the last one. Note that there could be a colon in the text string to the right but I would want to capture everything in the text string.

4 Answers

Following your logic:

re.search(r'\[.*?\]\s*\w+:\s*(.+)', temp).group(1)

See proof

Explanation

--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  \]                       ']'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  :                        ':'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1

You can exclude matching the :, then match it and optional whitespace chars. Then capture all that follows in a capturing group.

^\[[^][]*][^:]*:\s*(.+)

Regex demo

import re

regex = r"^\[[^][]*][^:]*:\s*(.+)"
temp = "[0:00:02] name1: Okay, this is the continued string..."
matches = re.search(regex, temp)

if matches:
    print(matches.group(1))

Output

Okay, this is the continued string...

Matching all 3 parts in a single pattern:

^\[([^][]*)]([^:]*):\s*(.+)

Explanation

  • ^\[ Match opening [ at the start of the string
  • ([^][]*) Capture group 1, match any char except [ and ]
  • ]\s* Match closing ]
  • ([^:]*) Capture group 2 Match any char except :
  • :\s* Match : and 0+ whitespace chars
  • (.+) Capture group 3, Match the rest of the string

regex demo

import re

regex = r"^\[([^][]*)]\s*([^:]*):\s*(.+)"
temp = "[0:00:02] name1: Okay, this is the continued string..."
matches = re.search(regex, temp)

if matches:
    print(matches.group(1))
    print(matches.group(2))
    print(matches.group(3))

Output

0:00:02
name1
Okay, this is the continued string...

You can literally match the timestamp with \[\d+:\d+:\d+\] and the first colon with .*?:

'\[\d+:\d+:\d+\].*?:(.*)'

You can actually match all your 3 groups with one regex:

'\[(\d+:\d+:\d+\)] (.*?):(.*)'

Let us get all together in a simple way g=re.findall(r'\[(.*?)\]\s*(.*):\s*(.*)',text)

import re

text='[0:00:02] name1: Okay, this is the continued string...'

g=re.findall(r'\[(.*?)\]\s*(.*):\s*(.*)',text)

time_frame = g[0][0]
speaker_id = g[0][1]
speach = g[0][2]

print(time_frame)
print(speaker_id)
print(speach)

output

0:00:02
name1
Okay, this is the continued string...
Related