Match beginning of a line and beginning of remain line

194 Views Asked by At

I'm trying come up with syntax highlighter that would match beginning of the line (timestamp) and then beginning of the remaining line. For example:

12:34:56.789 some1 text some2 other text
some3 text some4 other text

I need to capture words some but only if it's at the beginning of the text, ignoring timestamp. So in this example they are some1 and some3

{
  "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
  "name": "my-output",
  "scopeName": "source.my_output",
  "patterns": [
    {
      "begin": "^(\\d{2}:\\d{2}:\\d{2}\\.\\d{3,}\\s)?",
      "end": "$",
      "beginCaptures":{
        "1": {"name": "my-output-date"}
      },
      "patterns": [
        
        {
          "match": "^(some\\d)",
          "captures":{
            "1": {"name": "my-output-red"}
          }
        }
      ]
    }
  ]
}

The problem is beginning of the line may start with a timestamp 12:34:56.789 so in this example it only captures some3

If I remove ^ from the regex: "match": "(some\\d)" it captures all 4 words instead.

Does vscode provide ability split text into chunks and process each chunk as whole text (where we could use ^ and $ on the chunk)?

1

There are 1 best solutions below

0
vanowm On

Finally, I found a solution: use \G anchor (beginning of text after last match):

{
  "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
  "name": "my-output",
  "scopeName": "source.my_output",
  "patterns": [
    {
      "begin": "^(\\d{2}:\\d{2}:\\d{2}\\.\\d{3,}\\s)?",
      "end": "$",
      "beginCaptures":{
        "1": {"name": "my-output-date"}
      },
      "patterns": [
        
        {
          "match": "\\G(some\\d)",
          "captures":{
            "1": {"name": "my-output-red"}
          }
        }
      ]
    }
  ]
}

However, there is seems to be a bug where it skips a line if previous line is empty. For now, my fix is to replace "end": "$" regex with: "end": "\\r?\\n"