使用pyUNO搜索字符串和换行符 [英] Search strings and line breaks with pyUNO

查看:129
本文介绍了使用pyUNO搜索字符串和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文档中删除特定的字符串.我设法删除了字符串的内容,但是换行符仍然保留.我发现了有关 ControlCharacters ,但似乎它们只是数字常量.它真的有用吗?

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. Is it actually useful?

这有效.

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

这不是

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?

推荐答案

根据内置帮助:

使用正则表达式进行的搜索只能在一个 段落.使用多个中的正则表达式进行搜索 段落,请在每个段落中进行单独搜索.

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

这篇关于使用pyUNO搜索字符串和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆