Language package: what regex to count whitespace characters?


#1

Hello,
I am creating new language package, i want to highlight keyword after exactly one, two and three whitecharacter.
with regex ‘match’: ‘^\s{0,2}(system|System)’ it seem i only match the the keyword without any whitecharacter not after exactly two character .

Appreciate if you can shed some light here.

Here is the string:

  system
    system
      system
        system
  System
    System
      System
        System

And Here is my cson snippet:

  'stanza-1':
    'patterns': [
      {
        'match': '\^\s{0,1}(system|System)'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 1'
      }


  'stanza-2':
    'patterns': [
      {
        'match': '\^\s{0,2}(system|System)'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 2'
      }

  'stanza-3':
    'patterns': [
      {
        'match': '\^\s{0,3}(system|System)'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 3'
      }

#2

The ^ doesn’t need a \

\s{0, 2} means the number of space is from 0 to 2, instead of exactly 2.

/^ {2}[Ss]ystem/ will work :slight_smile:


#3

Thanks… Appreciate :slight_smile:

I but still the regex didn’t catch the string in atom…

i tried:

  'stanza-1':
    'patterns': [
      {
            'match': '\^\s{1}[Ss]ystem'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 1'
      }


  'stanza-2':
    'patterns': [
      {
            'match': '\^\s{2}[Ss]ystem'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 2'
      }

  'stanza-3':
    'patterns': [
      {
        'match': '\^\s{3}[Ss]ystem'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 3'
      }

and

      'stanza-1':
        'patterns': [
          {
            'match': '\^ {1}[Ss]ystem'
            'name':  'storage.type.function.system'
            'comment': 'config stanza 1'
          }


      'stanza-2':
        'patterns': [
          {
            'match': '\^ {2}[Ss]ystem'
            'name':  'storage.type.function.system'
            'comment': 'config stanza 2'
          }

      'stanza-3':
        'patterns': [
          {
            'match': '\^ {3}[Ss]ystem'
            'name':  'storage.type.function.system'
            'comment': 'config stanza 3'
          }

#4

You need \\s, not \s. Everything must be double-escaped in CSON files (if you want to know why, I can elaborate).
^\\s{1}[Ss]ystem should work.


#5

It did not work either

  'stanza-1':
    'patterns': [
      {
         'match': '^\\s{1}[Ss]ystem'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 1'
      }


  'stanza-2':
    'patterns': [
      {
        'match': '^\\s{2}[Ss]ystem'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 2'
      }

  'stanza-3':
    'patterns': [
      {
        'match': '^\\s{2}[Ss]ystem'
        'name':  'storage.type.function.system'
        'comment': 'config stanza 3'
      }

or

      'stanza-1':
        'patterns': [
          {
             'match': '\^\\s{1}[Ss]ystem'
            'name':  'storage.type.function.system'
            'comment': 'config stanza 1'
          }


      'stanza-2':
        'patterns': [
          {
            'match': '\^\\s{2}[Ss]ystem'
            'name':  'storage.type.function.system'
            'comment': 'config stanza 2'
          }

      'stanza-3':
        'patterns': [
          {
            'match': '\^\\s{2}[Ss]ystem'
            'name':  'storage.type.function.system'
            'comment': 'config stanza 3'
          }

#6

Mind posting the entire grammar file?