0

I am trying to test for an expression but not to select it. I need that for selecing custom TODOs in the IDE Pycharm. I want to select comments that have the word to-cleanup in them.

When I do the following: # \b.*to-cleanup\b.* it also selects the #. I'm pretty sure there must be a way to test for the existence of # but not to select it.

I just read the documentation for regex that Pycharm Help has, so I don't know how to do it. Any help would be greatly appreciated!

I checked here, but couldn't understand how to fit it into what I need.

Community
  • 1
  • 1
Amit Gold
  • 727
  • 7
  • 22

1 Answers1

1

You can use a positive lookbehind:

(?<=#) .*\bto-cleanup\b.*

The regex matches (see demo):

  • (?<=#) - a space preceded with a # symbol
  • .* - 0 or more characters other than a newline up to the last
  • \bto-cleanup\b - whole word to-cleanup
  • .* - 0 or more characters other than a newline (up to the end of the line).

This lookbehind is fixed-width and only checks if the space is preceded with # while the # itself is not part of the match.

See lookarounds details at regular-expressions.info

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563