Matching the first line after a bracket in Lint

0

I'm making a custom rule for programming language Lint to :

  • match 1 or more empty lines after {, and
  • another rule to match empty line before }.

As an example in this code, I want these rules to match line 2 and line 5 :

class Test {                   /* Line 1 */
                               /* Line 2 */
  func example() {             /* Line 3 */
  }                            /* Line 4 */
                               /* Line 5 */
}

I had tried to do it with positive lookahead/lookbehind but with no luck (?<=\{)\n.

Could anyone help with it ?

Update:

Added whitespeace to the example.

class Test { 

  func example() { 
  }

}

Feras Alnatsheh

Posted 2018-05-16T14:47:04.630

Reputation: 103

@C0deDaedalus, you created the [lint] tag. I'm waiting for questions using it like, "there's a lot of lint clogging the fan and my system is overheating". Can you add at least a wiki excerpt with usage guidance on the tag (a wiki entry would also be good)? :-) – fixer1234 – 2018-05-16T20:57:02.403

@fixer1234, sure would do that. Thanks for your concern. – C0deDaedalus – 2018-05-17T01:21:24.940

Answers

1

This is matching what you want:

\{\h*\R\K\h*\R|\R\K\h*\R(?=\h*\})
//added__^^^

Explanation:

  \{    : open brace
  \h*   : 0 or more horizontal spaces
  \R    : any kind of line break
  \K    : forget all we have seen until this position
  \h*   : 0 or more horizontal spaces
  \R    : any kind of line break
|       : OR
  \R    : any kind of line break
  \K    : forget all we have seen until this position
  \h*   : 0 or more horizontal spaces
  \R    : any kind of line break
  (?=   : positive lookahead
    \h* : 0 or more horizontal spaces
    \}  : close brace
  )     : end lookahead

Toto

Posted 2018-05-16T14:47:04.630

Reputation: 7 722

That's exactly what I was wanted, thank you! I appreciate it. – Feras Alnatsheh – 2018-05-16T18:04:53.060

If the empty line contains whitespace it won't detect it. Any suggestion how to do it? I provided new sample in the answer. – Feras Alnatsheh – 2018-05-16T19:25:20.680

1@FerasAlnatsheh: I missed a \h*, it was noted in explanation but not present in online regex. See my edit – Toto – 2018-05-16T20:36:14.760