skip to Main Content

I’m following this official guide on how to set a custom TODO filter in Android Studio.

I need to implement an highlight for important comments that are not TODOs, like these:

// important: this note is much more important than other comment lines
// !!!: this note is also much more important than other comment lines
// normal comment

With this regex it works:

bimportantb.*

How to set the filter with !!!?

I tried these but without success:

b!+b.*
b!!!b.*
b!+b.*
b!+b.*
b!!!b.*
b(important|x0021)+b.*

2

Answers


  1. What you are missing is that ! counts as a symbol and not as a part of a word, hence b does not see/identify a word boundary around it. You would need to use something like !!!.* to capture the line with 3 !

    Adding proof from regex101:
    enter image description here

    secondary regex based on comments –

    (bimportant:|!!!)(?!Bw).*
    

    which covers both use cases.

    Login or Signup to reply.
  2. The point is that your two alternatives start/end with different type of characters, word and non-word, and in this case you cannot blindly rely on the word boundaries, b.

    It appears your objective is to match two fixed alternatives, important and !!! as "whole words", but also be able to add more alternatives. In this case, you can use

    (bimportant|!!!)(?!Bw).*
    

    Here, bi will match i only at the start of string or right after a non-word char, and (?!Bw) will require a word boundary only if the preceding matched char is a word character, else, no word boundary will be required.

    Here are examples how you can extend the pattern to include c++:

    (bimportant|!!!|bc++)(?!Bw).*
    

    Here are examples how you can extend the pattern to include --args:

    (bimportant|!!!|--args)(?!Bw).*
    

    The most generic solution is to use (?!Bw) both at the start and end:

    (?!Bw)(important|!!!)(?!Bw).*
    

    That way, you do not lose much efficiency but you gain flexibility and you do not need to care about which alternatives start or end with non-word chars.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search