tell application "BBEdit"
set vDocument to first text document of first window
set vTimeRegex to "(?:[01][0-9]|2[0-3]):[0-5][0-9]"
set vRegex to "^(" & vTimeRegex & ")-(" & vTimeRegex & ")\\h+([A-Z]{3})"
set vOptions to {search mode:grep, starting at top:true, wrap around:false, returning results:true}
set vResult to find vRegex searching in vDocument options vOptions
if vResult's found then
repeat with vMatch in found matches of vResult
log vMatch
set vOptions to {search mode:grep, starting at top:true, wrap around:false, returning results:false}
set vTag to characters (vMatch's start_offset) thru (vMatch's end_offset) of vDocument
set vTagMatch to find vRegex searching in vTag options vOptions
if vTagMatch's found then
set vStartTime to grep substitution of "\\1"
set vEndTime to grep substitution of "\\2"
set vClientCode to grep substitution of "\\3"
log {vStartTime:vStartTime, vEndTime:vEndTime, vClientCode:vClientCode}
end if
end repeat
end if
end tell
tell application "BBEdit"
set vDocument to first text document of first window
set vTimeRegex to "(?:[01][0-9]|2[0-3]):[0-5][0-9]" -- Regex to match any time from 00:00 to 23:59.
set vRegex to "^(" & vTimeRegex & ")-(" & vTimeRegex & ")\\h+([A-Z]{3})" -- Regex to captures 3 informations:
-- at beginning of a line
-- a start time to be captured
-- a colon
-- a end time to be captured
-- one or more horizontal spaces
-- 3 letters to be captured
set vOptions to {search mode:grep, starting at top:true, wrap around:false, returning results:true} -- We want `find` to return a list of the found matches with offsets in the document.
set vResult to find vRegex searching in vDocument options vOptions
log vResult
if vResult's found then -- The find command succeeded. vResult has a `found matches` property that lists the matches.
repeat with vMatch in found matches of vResult -- Lets loop over each match and extract the captured strings.
log vMatch
set vOptions to {search mode:grep, starting at top:true, wrap around:false, returning results:false} -- This time, we don't want find to return results because we want to extract the captured strings.
set vTag to characters (vMatch's start_offset) thru (vMatch's end_offset) of vDocument -- Create a reference to the substring we want to decompose by using grep substitutions.
set vTagMatch to find vRegex searching in vTag options vOptions -- Use `find` again against each substring (e.g. '08:30-11:30 UWA') to capture the matching parts.
if vTagMatch's found then -- This should always succeed.
set vStartTime to grep substitution of "\\1" -- matched by the first capturing parentheses: '08:30'
set vEndTime to grep substitution of "\\2" -- matched by the second capturing parentheses: '11:30'
set vClientCode to grep substitution of "\\3" -- matched by the third capturing parentheses: 'UWA'
log {vStartTime:vStartTime, vEndTime:vEndTime, vClientCode:vClientCode}
end if
end repeat
end if
end tell