What's the regular expression for capturing something surrounded by asterisks inside a comment?

232 Views Asked by At

I'm trying to modify an existing language definition that defines comments like this:

    <dict>
        <key>match</key>
        <string>(#) .*$\n?</string>
        <key>name</key>
        <string>comment.line.number-sign.myLanguage</string>
        <key>captures</key>
        <dict>
            <key>1</key>
            <dict>
                <key>name</key>
                <string>punctuation.definition.comment.myLanguage</string>
            </dict>
        </dict>
    </dict>

This ensures that a line starting with a # will be identified as a comment and highlighted accordingly. What I would like to do is surround a commented word in asterisks and have it show up as some other thing, a keyword for example. But simply appending this doesn't work:

    <dict>
        <key>match</key>
        <string>\*([^*]+)\*</string>
        <key>captures</key>
        <dict>
            <key>1</key>
            <dict>
                <key>name</key>
                <string>keyword.myLanguage</string>
            </dict>
        </dict>
    </dict>

However, it does work if the line doesn't start with a #, so I'm assuming there is a conflict between both rules. So, I thought I could get around it by using a regex that identifies everything in the comment that isn't surrounded by *, something like:

# This is a comment *this is something else* this is still a comment *not* yes

Any ideas?

1

There are 1 best solutions below

1
On BEST ANSWER

Ok, I got it to work:

<dict>
  <key>begin</key>
  <string>(#) .*</string>
  <key>name</key>
  <string>comment.line.number-sign.myLanguage</string>
  <key>captures</key>
  <dict>
      <key>1</key>
      <dict>
          <key>name</key>
          <string>punctuation.definition.comment.myLanguage</string>
      </dict>
      <key>0</key>
      <dict>
          <key>patterns</key>
          <array>
            <dict>
              <key>name</key>
              <string>keyword.myLanguage</string>
              <key>match</key>
              <string>\*[^*]+\*</string>
            </dict>
          </array>
      </dict>
  </dict>
  <key>end</key>
  <string>$\n?</string>