如何在GtkSourceBuffer中进行正则表达式搜索

问题描述 投票:1回答:1

我正在使用GtkSourceViewGtkSourceBuffer

我需要对其内容进行正则表达式搜索,我知道GtkSourceBufferGtkTextBuffer的子类。

我想做类似下面的Python代码,其中search_text是一个正则表达式。

search_text = 'some regular expression'
source_buffer = source_view.get_buffer()
match_start = source_buffer.get_start_iter()
result = match_start.forward_search(search_text, 0, None)
if result:
    match_start, match_end = result
    source_buffer.select_range(match_start, match_end)

正则表达式并不太复杂:search_text = '/file_name\S*'。 (基本上我想匹配文档中的所有文件名,前面有分隔符/,以公共文件名开头,并以一系列非空格字符结尾,包括文件扩展名)。

Gtk.GtkTextIter.forward_search()函数似乎只接受这三个标志,所以我没有看到指定搜索字符串是正则表达式的方法......

Gtk.TextSearchFlags.VISIBLE_ONLY
Gtk.TextSearchFlags.TEXT_ONLY
Gtk.TextSearchFlags.CASE_INSENSITIVE

如何在GtkSourceBuffer或GtkTextBuffer上实现正则表达式搜索?

regex python-3.x gtk3
1个回答
1
投票

你应该看看SearchSettings,它允许你enable regexset search text。之后你创建一个SearchContext并用它来搜索(forwardbackward方法)

此外,GktTextBuffer可以使用get_text返回它的文本,但它不是你想要的。

© www.soinside.com 2019 - 2024. All rights reserved.