Skip to content

Instantly share code, notes, and snippets.

@kbloom
Created December 29, 2010 01:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbloom/757999 to your computer and use it in GitHub Desktop.
Save kbloom/757999 to your computer and use it in GitHub Desktop.
OpenOffice Calc regex matching macro
Function regex(a,b,c)
' Attention - made by novice, so can contain bugs
' a - string or cell to search in
' b - regexp string or cell containing regexp string
' c - back-reference number - analogy to \n in regexp syntax
' prepare regexp search options
oTextSearch = CreateUnoService("com.sun.star.util.TextSearch")
oOptions = CreateUnoStruct("com.sun.star.util.SearchOptions")
oOptions.algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
oOptions.searchString = b
oTextSearch.setOptions(oOptions)
' search first substring
oFound = oTextSearch.searchForward(a, 0, Len(a))
If oFound.subRegExpressions=0 then
regex = "No result in that back-reference level"
Exit Function
Else
nStart = oFound.startOffset()
nEnd = oFound.endOffset()
regex = Mid(a, nStart(c) + 1, nEnd(c) - nStart(c))
End If
End Function
@vmalep
Copy link

vmalep commented Jul 21, 2017

Nice macro. Would be nice to convert it into an LO extension...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment