Jacob Krieg
Jacob Krieg

Reputation: 3164

RegExp: Match all comments that don't end with period

I want to find all the commented lines that don't end with period(.).

I have this expression //[A-Za-z0-9-\(\)\.,\-\\/ !\:_"] but I don't know how to make it detect only lines that don't have periond(.) before EOL.

I'm using C++ so comments start with //. For example I want this string to be matched:

// My comment here

but not this one:

// My comment here.

Anyone can help?

Upvotes: 1

Views: 691

Answers (2)

anubhava
anubhava

Reputation: 785128

This negative lookbehind based regex should work for you:

(?m)^//.*$(?<!\.)

OR negative lookahead based:

(?m)^//(?!.*\.$).*$

Live Demo: http://www.rubular.com/r/sUHNDokY2j

Upvotes: 2

ohaal
ohaal

Reputation: 5268

No need for negative lookbehind or lookahead, keep it simple:

^\/\/(.*[^.])$
        \    /
         \  /
          \/
           `-- This is the key, match any non-period character at the end.

Demo: http://rubular.com/r/JZb3RehqrR

As anubhava pointed out in the comment, the above regex does not capture empty comment lines. If you wish to also capture the empty comment lines, you can add a ? to make the comment optional:

^\/\/(.*[^.])?$
             ^
             |
             `-- Added this in order to make the comment optional

Demo: http://rubular.com/r/INyArTf9qQ

Upvotes: 4

Related Questions