我正在使用GtkSourceView
和GtkSourceBuffer
。
我需要对其内容进行正则表达式搜索,我知道GtkSourceBuffer
是GtkTextBuffer
的子类。
我想做类似下面的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上实现正则表达式搜索?
你应该看看SearchSettings,它允许你enable regex和set search text。之后你创建一个SearchContext并用它来搜索(forward
或backward
方法)
此外,GktTextBuffer可以使用get_text返回它的文本,但它不是你想要的。