0

I have source code, and I would like to find all lines than end with comma, dot or plus symbols ',' , '.' , '+',

So, I have this: (\,|\.|\+)$ - to show lines that end with any of these symbols.

But I want to ignore lines that start with // - commented lines, and they can be preceded with any number of spaces.

I tried this to ignore lines with / at the beginning: ^[^/].* but this doesn't really work well. Then I also tried to put: \s* at the beginning to ignore all spaces, but still not working good.

So, my regex would be something like: \s*^[^/].*(\,|\.|\+)$ - skip all spaces at the beginning AND ignore those with / as first symbol AND show those lines that end with ,.+

But this still finds lines with that start with space and /. What am I doing wrong?

EDIT: Here are example lines:

...
// increase no of objects...
counter := 1+
2;
...

I need to ignore comment lines, starting with // but need all lines that end with .,+ as I need to reformat them.

Mike Torrettinni
  • 1,816
  • 2
  • 17
  • 47
  • 2
    You're going to do much better with a multi-pass approach -- on the command line, I'd do this with `grep -v` to filter out comments, and then a second `grep` that looks for lines you actually *do* want. Otherwise, you'll need to use PCRE extensions, thus requiring a backtracking regex engine, which will potentially be *slower* than just applying two BREs or EREs in turn. – Charles Duffy Oct 22 '16 at 16:34
  • What is the first `^` for, in that last regex? Did you mean `\s*[^/].*(\,|\.|\+)$`? I'm not sure if that would work either, depending on greediness... – LarsH Oct 22 '16 at 16:34
  • Also, re: Charles comment about backtracking, what regex dialect are you using? (What regex tools?) That would help us know what capabilities are available. – LarsH Oct 22 '16 at 16:36
  • I use Regular expression search option in Delphi IDE - not sure what is in the background. – Mike Torrettinni Oct 22 '16 at 16:37
  • @LarsH I tried your example (not sure where I got the first `^`) but still finds lines like ` // text...`. – Mike Torrettinni Oct 22 '16 at 16:42
  • 1
    @Mike, try `\s*[^\s/].*(\,|\.|\+)$`. It's not perfect in theory but it should probably work for your purposes. – LarsH Oct 22 '16 at 16:49
  • @LarsH still picking up comment lines. Didn't know this was so hard to do. – Mike Torrettinni Oct 22 '16 at 17:07
  • Sorry, put a `^` on the front of it: `^\s*[^\s/].*(\,|\.|\+)$` – LarsH Oct 22 '16 at 17:47
  • Still the same, it finds // lines. – Mike Torrettinni Oct 22 '16 at 17:56
  • 1
    Just as a side note, the IDE uses a regex engine that supports far less than Delphis TRegex - got confused by that myself. It might be a better idea to use something like Notepad++, Sublime or a custom Delphi program to do this task. – Sebastian Proske Oct 22 '16 at 18:18
  • 1
    Here is the [IDE regex docu](http://docwiki.embarcadero.com/RADStudio/Seattle/en/Regular_Expressions). IIRC GExperts has support for grep, maybe that is also worth a try. – Sebastian Proske Oct 22 '16 at 18:25

2 Answers2

2

The only way to do this with one single regex will be to use a negative lookbehind. However, you would end up with a regex that starts with ^(?<!\s*\/), which is invalid due to the quantifier in the lookbehind.

That means the only way that actually works is to use multiple regexes.

The lines you want will match this (which literally means "a comma, a dot or a plus sign at the end of the line"):

[,.+]$

but not this (which literally means "zero or more spaces followed by a forward slash at the beginning of the line"):

^\s*\/
The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27
2

Maybe it can works:

^(?!/|\s*/)+.*[\,\.\+]$

This regular expression ignores all lines who start with / or zero or more spaces follow by / and ends with ,, ., or +.

I hope works for you.

EDIT: It can be reduced:

^(?!\s*/)+.*[\,\.\+]$

I tested this regular expression, how you can see, it ignores line 192 and match 210, but it doesn't ignore lines inner a block comment, because it doesn't start with /.

Test for regular expression

AlexR
  • 100
  • 7