Free版Office calc Basic 正規表現でStringを検索 全マッチ関数

LibreOffice calcのBasicで正規表現による 文字列の検索 関数ラッパ
マッチ文字列の配列を返す関数をつくりました。


	dim arrMatch()
		matchexp(oStrIni,oSch,arrMatch)


Function matchexp(src as string,searchstr as string,arrMatch  ) as Integer

	Dim oTextSearch as Object
	Dim oOption as Object
	Dim searchStart as Integer

	ReDim Preserve  arrMatch(0)
	
	arrMatch(0)=src
	oTextSearch = CreateUnoService("com.sun.star.util.TextSearch")
	oOption = CreateUnoStruct("com.sun.star.util.SearchOptions")

	oOption.algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
	oOption.searchFlag = com.sun.star.util.SearchFlags.REG_EXTENDED
	oOption.searchString = searchstr

	oTextSearch.setOptions(oOption)

	searchStart=0
	do while  searchStart < Len(src)
		oResult = oTextSearch.searchForward(src,searchStart,Len(src))
  		If oResult.subRegExpressions = 1 Then
  			searchStart = oResult.endOffset(0) 			
			
			ReDim Preserve  arrMatch(UBound( arrMatch )+1)
			
			arrMatch(UBound(arrMatch))=mid(src,oResult.startOffset(0) +1,oResult.EndOffset(0) -oResult.startOffset(0))
	elseif oResult.subRegExpressions > 1 Then
			for i=0 to ubound(oResult.startOffset)
				ReDim Preserve  arrMatch(UBound( arrMatch )+1)
				arrMatch(UBound(arrMatch))=mid(src,oResult.startOffset(i) +1,oResult.EndOffset(i) -oResult.startOffset(i))
			
			next		
			searchStart=Len(src)
    	else
			searchStart=Len(src)
  		End If
	
	loop

	matchexp=ubound(arrMatch)


end function