在Ubuntu 12.04上使用Libreoffice 3.5.7.2。
我在calc细胞中有以下形式的文本:(IBM)Ibm Corporation。
我正在尝试使用正则表达式使用基本宏在()之间提取文本。这是我到目前为止所尝试过的。
Sub getMktValue()
Dim oDoc as Object
Dim oSheet as Object
Dim oCell as Object
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByName("Income")
'regex test code'
oCell = oSheet.getCellByPosition(0, 1)
stk = oCell.String()
myRegex = oCell.createSearchDescriptor
myRegex.SearchRegularExpression = True
myRegex.SearchString = "\((.*)\)" '"[\([A-Z]\)]" "\(([^)]*)\)" "\(([^)]+)\)"'
found = oCell.FindFirst(myRegex)
MsgBox found.String
End Sub
myRegex.SearchString行包含我尝试过的各种版本。结果总是一样的。单元格的全部内容不仅返回()之间的文本。有没有办法在()之间提取文本?
谢谢,吉姆
答案 0 :(得分:1)
.FindFirst在电子表格中找到包含SearchString的Cell。如果要在字符串中搜索,则需要com.sun.star.util.TextSearch。
Sub getMktValue()
Dim oDoc as Object
Dim oSheet as Object
Dim oCell as Object
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByName("Income")
'regex test code'
oCell = oSheet.getCellByPosition(0, 1)
stk = oCell.getString()
oTextSearch = CreateUnoService("com.sun.star.util.TextSearch")
oOptions = CreateUnoStruct("com.sun.star.util.SearchOptions")
oOptions.algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
oOptions.searchString = "\((.*)\)"
oTextSearch.setOptions(oOptions)
oFound = oTextSearch.searchForward(stk, 0, Len(stk))
sFound = mid(stk, oFound.startOffset(0) + 1, oFound.endOffset(0) - oFound.startOffset(0))
MsgBox sFound
sFound = mid(stk, oFound.startOffset(1) + 1, oFound.endOffset(1) - oFound.startOffset(1))
MsgBox sFound
End Sub
问候
阿克塞尔