I'm trying to comment out some lines in c++ by matching them with a regex, and replacing with the match preceeded by \\. But instead, gedit performs no replacement, and the search+replace dialog gives a little ! by the replace field.

The regular expression I'm using is ^(.*kBlack[^\n]+)\n which matches correctly (any line with kBlack in). My replacement is \\\\\1\n - i.e. \\ to escape the second \ and produce a single \, repeated for two \'s, followed by \1 for the submatch, then a newline \n. But no luck.

No variation in the number of starting slashes seems to give me what I'm looking for. What are the rules for properly inserting backslashes?

  • Not an answer, but: why don’t you just use //? – Scott Sep 22 '17 at 23:34
  • ............................. oh my god. I can't believe I've been trying to insert \'s for a C++ comment, when a comment is // . Well, ... I suppose it would still be nice to know. Thanks for pointing out that blunder though! – Marcus S O'Flaherty Sep 23 '17 at 0:19
  • Probably a bug in the regex engine (see glib/gregex.c)... If you place your cursor over that exclamation mark for a couple of seconds it will pop up the exact error message... Your regex & replacement work fine here with v. 2.52. However, it looks like this version isn't bug-free either as trying to replace any char with a backslash (that is, escaping it, so using a double backslash as the replacement) results in a stray final \ error... – don_crissti Sep 23 '17 at 11:12
  • Actually, your replacement doesn't work properly with v. 2.52 as it inserts only one backslash... to insert two backslashes I have to use \\\\\\\\\1\n... lol... what a mess... – don_crissti Sep 23 '17 at 11:41
  • Yikes! I mean i fiddled with a variety of attempts, but i don't think i tried that many backslashes! Thanks for pointing to the file, if this becomes a real issue I'll know where to look. – Marcus S O'Flaherty Oct 20 '17 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.