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
What you are missing is that
!
counts as a symbol and not as a part of a word, henceb
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:
secondary regex based on comments –
which covers both use cases.
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 useHere,
bi
will matchi
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++
:Here are examples how you can extend the pattern to include
--args
:The most generic solution is to use
(?!Bw)
both at the start and end: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.