0

I am trying to mass comment out all the lines referring to logging in a code. I am using notepad ++ and ideally this would be achievable by replacing all the lines being like

< some text > logging

by

# < some text > logging

Sorry in advance to ask this kind of question on SO, but I'm kind of stuck , and I'm sure this can be useful to other people.

jim jarnac
  • 4,804
  • 11
  • 51
  • 88

3 Answers3

1

Find with (?=.*logging)^ and replace with #

Eplanation:

(?=.*logging) will lookahead for word logging and if present search for beginning of string with ^

Notepad ++ Regex replace.

Regex101 Demo

Rahul
  • 2,658
  • 12
  • 28
  • @jimbasquiat: It's a Regex Tester. You enter your regex in first input box and content to search in second(which is large). Optional substitution can be done in third box which appears when you click on **Substitution** at bottom. – Rahul May 01 '17 at 11:18
  • ok i see. Is there any site that you know that can create the regex? – jim jarnac May 01 '17 at 11:27
  • @jimbasquiat: TL;DR No ! – Rahul May 01 '17 at 11:29
  • Well, there actually exist some. Like for example [this](http://buildregex.com/). But learning regex really isn't that hard for most of the stuff. And you can learn also from the explain plan on a site like that regex tester. – LukStorms May 01 '17 at 11:34
  • @LukStorms: That one is good. But still needs user input in creating regex. – Rahul May 01 '17 at 11:38
  • Oh so you mean there's none that can discover the pattern from a list of strings and guess a regex for it. Yeah, probably not. That would need an AI... – LukStorms May 01 '17 at 11:43
  • @LukStorms: Exactly ! – Rahul May 01 '17 at 13:02
  • Then again. This is stackoverflow, and ... [it's been asked before](http://stackoverflow.com/questions/3196049/regular-expression-generator-reducer). [This one](http://kemio.com.ar/tools/lst-trie-re.php) seems to work even. O_o – LukStorms May 01 '17 at 14:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143104/discussion-between-rahul-and-lukstorms). – Rahul May 01 '17 at 17:05
1

In notepad++,

Search "^(.*) logging" Replace "#\1 logging"

Make sure search mode is "Regular expression"

Srihari Karanth
  • 2,067
  • 2
  • 24
  • 34
1

Find what : ^.*logging$
Replace with: # $0
Search mode: Regular expression

$0 is a variable for the match.
And the regex matches a line that ends with "logging". So replacing it with # $0 puts a # at the start of a line that ends with logging.

^ : start of a line
.* : zero or more characters
$ : end of a line

And to a avoid commenting lines that are already are commented?
Adding a negative lookahead helps with that:

Find what: ^(?!\s*#).*logging$

LukStorms
  • 28,916
  • 5
  • 31
  • 45