使用这个简短的 XML 片段在 gtksourceview 中定义字符串:
<context id="string" style-ref="string" end-at-line-end="true">
<start>"</start>
<end>"</end>
<include>
<context id="special-string" style-ref="special">
<match>(foo|bar)</match>
</context>
<context ref="def:line-continue"/>
</include>
</context>
这会突出显示字符串中“foo”或“bar”的任何实例。假设我想在这两个是整个字符串的时候高亮显示,这样“foo”就把中间的三个字符高亮了,而“foobar”根本就没有高亮显示。这在 gtksourceview 中可能吗?
我尝试使用 anchor ^...$
和向前看/向后看(?<=")
/(?=")
但前者不起作用,后者完全破坏了语法荧光笔。
最佳答案
似乎没有办法在子上下文中匹配父上下文的开头(或结尾)。但是,如果您将 special-string 定义为顶级上下文并将其包含在主要语言上下文中的 string 之前,它将起作用:
<context id="special-string" style-ref="string">
<match>"(foo|bar)"</match>
<include>
<context sub-pattern="1" style-ref="special"/>
</include>
</context>
<context id="string" style-ref="string" end-at-line-end="true">
<start>"</start>
<end>"</end>
</context>
关于regex - 在 gtksourceview 中匹配整个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237471/