Ghoul Fool
Ghoul Fool

Reputation: 6967

Sublime colour syntax highlighting using regex

I've managed to put together a syntax (.tmLanguage) file for use in Sublime Text 2. I'd quite like to highlight numerals. I tried:

<string>0|1|2|3|4|5|6|7|8|9</string>

which works, but only for single digits, so I thought the regex would be

<string>[0-9]</string>

But that doesn't work. Can someone please help me with the correct syntax in Sublime?

Upvotes: 0

Views: 210

Answers (1)

Mowday
Mowday

Reputation: 412

If you change your code to:

<string>\d+</string>

It should find all integers.

  • \d equals any number (0-9)
  • + Is a multiplier stating "one or more of the previous character"

In your case, at least one digit, but as many as possible. Might I suggest:

<string>\d+(\.\d+)?</string> 

as that will find decimal numbers as well.

  • \d equals any number (0-9)
  • + Is a multiplier stating "one or more of the previous character"
  • ( Starts a group
  • \. An escaped period sign, to actually capture the period character
  • \d+ One or more digits
  • ) End f the group
  • ? Makes the entire group optional.

That should capture both integers and decimal numbers.

Upvotes: 1

Related Questions