I have a document with very many time stamps like 19:23:45, and I want to subtract a fixed amount of time from each.
I am trying to do this using a macro, using a regular expression “(\d\d):(\d\d):(\d\d)”. I want to assign to three variables the actual text matched by each of the parenthesized groups in the regex. How can this be done? Does Libreoffice basic support Match objects like VB?
Easily done with a Python macro.
import re
match_obj = re.match("(\d\d):(\d\d):(\d\d)", "19:23:45")
hours, minutes, seconds = match_obj.groups()
For regular expressions in LO Basic, use the cumbersome com.sun.star.util.TextSearch service.
sTimestamp = "19:23:45"
oTextSearch = CreateUnoService("com.sun.star.util.TextSearch")
oOptions = CreateUnoStruct("com.sun.star.util.SearchOptions")
oOptions.algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
oOptions.searchString = "(\d\d):(\d\d):(\d\d)"
oTextSearch.setOptions(oOptions)
oFound = oTextSearch.searchForward(sTimestamp, 0, Len(sTimestamp))
nStart = oFound.startOffset()
nEnd = oFound.endOffset()
hours = Mid(sTimestamp, nStart(1) + 1, nEnd(1) - nStart(1))
minutes = Mid(sTimestamp, nStart(2) + 1, nEnd(2) - nStart(2))
seconds = Mid(sTimestamp, nStart(3) + 1, nEnd(3) - nStart(3))
MsgBox "hours = " & hours & ", minutes = " & minutes & ", seconds = " & seconds
However, a regex is not needed.
sTimestamp = "19:23:45"
splits = Split(sTimestamp, ":")
hours = splits(0)
minutes = splits(1)
seconds = splits(2)
MsgBox "hours = " & hours & ", minutes = " & minutes & ", seconds = " & seconds
The equivalent in Python is a single line of code.
hours, minutes, seconds = "19:23:45".split(":")
More explanation and examples can be found at: