CLM pattern question

34 views
Skip to first unread message

Tim Gray

unread,
Feb 15, 2022, 10:44:59 AM2/15/22
to BBEdit Talk
I have a question about defining keyword pattern matching in codeless language modules using BBLMKeywordPatterns. Can a pattern be defined to match only words at the beginning of a line? I’m not having much success using the standard ^ regular expression.

To give context, I’d like to define two types of run kinds. Both are always at the beginning of a line and end with a colon; one also leads off with a %. Example:

Keyword: do not match anything after the colon
%keywordtwo: don’t match this either

Thanks
Tim


jj

unread,
Feb 16, 2022, 7:02:12 AM2/16/22
to BBEdit Talk
Hi Tim,

As far as I understand it, BBLMKeywordPatterns are matched against individual words composed of characters in the <Identifier and Keyword Character Class> or <Identifier and Keyword Characters>.

As a consequence the ^ anchor in a BBLMKeywordPatterns is sort of equivalent to \b and will always match at the start of a word. 
Start of line for an individual word doesn't make much sense.

(Please Rich or Patrick, correct me if I am wrong)

You could kind of achieve what you are looking for by including the '%' and ':' characters in the <Identifier and Keyword Character Class> and matching in the BBLMKeywordPatterns with:

    keyword:
   
and

    %keywordtwo:
   
Watch out though that those sequences might be present in some other unwanted positions in the text, for example at the end of a line before a bullet list.

Another solution could be to use the <Keyword Pattern> which is matched against the text of the document and thus can detect ^ anchors at the start of lines.

    ...
    <key>Language Features</key>
    <dict>
    ...
    <key>Keyword Pattern</key>
        <string><![CDATA[(?x)
(
    ^keyword(?=:)
|
    ^%keywordtwo(?=:)
)
]]></string>
    ...
    <dict/>
    ...
   
HTH

Jean Jourdain

Tim Gray

unread,
Feb 16, 2022, 7:03:19 PM2/16/22
to BBEdit Talk
Thanks.  Option two was better for me.  I didn't realize the BBLMKeywordPatterns were for 'words' only.  I struggled a bit with getting commenting to get highlighted.  I ultimately had to define a 'Comment Pattern' as well.  Something about the 'Keyword Pattern' prevented the 'Open Line Comment' from working, at least how I defined them.  The two patterns were:

<key>Keyword Pattern</key>
<string><![CDATA[(?x)
^[^#]\w+(?=:)
]]></string>

<key>Comment Pattern</key
<string><![CDATA[(?x)
^\# .* $
]]></string>

Tim
--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages