I need a regular expression to uncomment a block of Perl code, commented with # in each line.
As of now, my find expression in the Eclipse IDE is (^#(.*$\R)+) which matches the commented block, but if I give $2 as the replace expression, it only prints the last matched line. How do I remove the # while replacing?
For example, I need to convert:
# print "yes";
# print "no";
# print "blah";
to
print "yes";
print "no";
print "blah";
In most flavors, when a capturing group is repeated, only the last capture is kept. Your original pattern uses
+repetition to match multiple lines of comments, but group 2 can only keep what was captured in the last match from the last line. This is the source of your problem.To fix this, you can remove the outer repetition, so you match and replace one line at a time. Perhaps the simplest pattern to do this is to match:
And replace with the empty string.
Since this performs match and replacement one line at a time, you must repeat it as many times as necessary (in some flavors, you can use the
gglobal flag, in e.g. Java there arereplaceFirst/Allpair of methods instead).References
Related questions
*and+?Special note on Eclipse keyboard shortcuts
It Java mode, Eclipse already has keyboard shortcuts to add/remove/toggle block comments. By default,
Ctrl+/binds to the “Toggle comment” action. You can highlight multiple lines, and hitCtrl+/to toggle block comments (i.e.//) on and off.You can hit
Ctrl+Shift+Lto see a list of all keyboard shortcuts. There may be one in Perl mode to toggle Perl block comments#.Related questions