简体   繁体   English

使用pyUNO搜索字符串和换行符

[英]Search strings and line breaks with pyUNO

I would like to delete a specific string from a document. 我想从文档中删除特定的字符串。 I manage to delete the content of the string, but the line break still remains after. 我设法删除字符串的内容,但是换行符仍然保留。 I found some things about ControlCharacters but it seems they are only numeric constants. 我发现了一些有关ControlCharacters的信息,但似乎它们只是数字常量。 Is it actually useful? 它真的有用吗?

This works. 这可行。

r = oDoc.createReplaceDescriptor()
r.setSearchString("FOOBAR")
r.setReplaceString("OTHERSTUFF")
oDoc.replaceAll(r)

This does not 这不

r = oDoc.createReplaceDescriptor()
r.setSearchString("FOOBAR\n")
r.setReplaceString("OTHERSTUFF")
oDoc.replaceAll(r)
r = oDoc.createReplaceDescriptor()
r.setSearchString("FOOBAR\r")
r.setReplaceString("OTHERSTUFF")
oDoc.replaceAll(r)

How do I delete the whole line, including the line break? 如何删除整行,包括换行符?

According to the built in help: 根据内置的帮助:

A search using a regular expression will work only within one paragraph. 使用正则表达式的搜索仅在一个段落内有效。 To search using a regular expression in more than one paragraph, do a separate search in each paragraph. 要在多个段落中使用正则表达式进行搜索,请在每个段落中进行单独的搜索。

I interpret this to mean that newline characters cannot be searched for. 我将其解释为无法搜索换行符。 Instead, loop through the search results and delete the character. 而是循环搜索结果并删除字符。 Here is some code that does this: 这是执行此操作的一些代码:

search = oDoc.createSearchDescriptor()
search.SearchRegularExpression = True
search.SearchString = "FOOBAR$"
selsFound = oDoc.findAll(search)
for sel_index in range(0, selsFound.getCount()):
    oSel = selsFound.getByIndex(sel_index)
    try:
        oCursor = oSel.getText().createTextCursorByRange(oSel)
    except (RuntimeException, IllegalArgumentException):
        return
    oCursor.setString("")  # delete
    oCursor.goRight(1, True) # select newline character
    oCursor.setString("")  # delete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM