Created
December 29, 2010 01:09
-
-
Save kbloom/757999 to your computer and use it in GitHub Desktop.
OpenOffice Calc regex matching macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice macro. Would be nice to convert it into an LO extension...