Regex : How to remove empty lines only in the first comment block?
I have a bunch of files all starting with a comment block such as this :
/**
* @author 开发者_StackOverflow社区AAA BBB
CCC DDDD
EEEE FFFF
* @date 2008-08-14
*/
Sometimes there is only 1 author line :
/**
* @author AAA BBB
* @date 2008-08-14
*/
I am trying to come up with a regular expression that would remove the empty lines between @author and @date, but not in the rest of the file.
What I have currently :
(@author.*$)([\s\S]*)(^.*@date)
This is of course not enough but all my attempts to insert a (^$) in there led to wrong selections or infinite loops.
What is the correct regular expression ?
I don't know which environment/tool/language you are going use but something on these lines should do the trick - in Vi/sed (not tested)
\/\*.*\([\*\t\s\S\n\r]*\)\(@author.*\).*\([\*\t\s\S\n\r]*\).*\(@date.*).*\*/\
This is what it means:
Look for a string that starts with /* can be followed by any character(s). It may contain any whitespace till it encounters '@author' again, characters/whitespaces and then '@date' ending with other characters and closing with */
You basically want to do "substring" regexing (if it's even a word) - look for a large pattern and extract sub patterns in it (demarcated by \( and \) - escaping round brackets.
You can then refer to the expression(s) positionally like \1 \2 and so on. Basically substring \(...\)everything you want to keep and just replace in the end using \1 \2 etc., This should work fine as long as this pattern doesn't repeat elsewhere :)
Hope this helps. It may not be "the perfect" regex but you get the idea of how to structure it and extract substrings...
In JEdit I finally got what I wanted by using the following regex :
(@author.*)([\s\S]*)(^$\n)( \* @date.*)
and the replacement string is
$1$2$4
If you have vim
installed and the @author
and @date
labels occur only once per file, you can do:
vim -e '/@author/,/@date/v:.:d' -e 'x' FILE
If you have several files, you should use:
vim -e 'buffdo!/@author/,/@date/v:.:d' -e 'xa' FILES
Vim will open the file(s), then search for the blockrange, then search for empty lines (a single space is not empty line!), then deletes them and write the file(s) and exits.
HTH
精彩评论