用逗号连接换行符并在Notepade ++中删除空行到换行符

时间:2015-11-06 07:43:51

标签: regex notepad++ line-breaks blank-line

我有一个像这样的文本文件......

$index 57320   
$title The vertex-cover polynomial of a graph  
$time 1988  
$abstract In this paper we define the vertex-cover...  

$index 57321   
$title Locating stations on rapid transit lines  
$time 1978  

$index 57322   
$title Fast heuristics for large scale covering-location problems  
$time 1998  
$abstract We propose fast heuristics for large scale...  

$index 57323   
$title Efficient vector processing on dataflow supercomputer SIGMA-1  
$time 2001  
$abstract Efficiency in vector handling is the key to obtaining high...  

我想将每个linebreak转换为comma,同时将每个emptyline转换为linebreak。而示例文本的输出应该是这样的(使用"点" ...缩短文本):

$index 57320,$title The vertex-cover...,$time 1988,$abstract In this paper...  
$index 57321,$title Locating stations on...,$time 1978  
$index 57322,$title Fast heuristics for...,$time 1998,$abstract We propose fast...  
$index 57323,$title Efficient vector...,$time 2001,$abstract Efficiency in...  

我尝试将\r\n替换为,并且它有效但是如何同时应用两种操作来将linebreaks转换为commaemptyline以用作linebreaks用于获得所需的输出。

请在这方面提供帮助。
谢谢!

2 个答案:

答案 0 :(得分:2)

放入查找&替换为正则表达式模式。

查找

([^\r\n]+)\r\n

替换为:

$1,

你可以找到这个,以摆脱每一行的尾随空格:

([^\r\n]+?) *\r\n

答案 1 :(得分:0)

您需要分两步完成。首先,用逗号替换所有换行符,但前提是它们不在行的开头,并且只有在$字符后面:

(?<!^)[ \t]*\r?\n(?=\$)

将所有这些匹配替换为,。注意[ \t]*部分用于清理每行末尾的空格 - 我发现在你发布的样本中;如果它不存在于现实中,你可以省略那部分。测试live on regex101.com

然后,将所有(\r?\n){2,}替换为$1

相关问题