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

2024-04-26 22:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从文档中删除特定的字符串。我仍然要删除字符串的内容,但仍要管理字符串的内容。我发现了一些关于ControlCharacters的东西,但似乎它们只是数值常量。它真的有用吗?在

这很管用。

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

这不

^{2}$

如何删除整行,包括换行符?在


Tags: 字符串文档内容数值foobar常量otherstuffreplaceall
1条回答
网友
1楼 · 发布于 2024-04-26 22:42:24

根据内置帮助:

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.

我的意思是不能搜索换行符。相反,循环搜索结果并删除字符。下面是一些代码:

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

相关问题 更多 >