0

I have a text file like this...

$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...  

I want to convert each linebreak into comma and in the same time each emptyline to linebreak. Whereas the output for example text should be like this(text shortened using "dots"...):

$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...  

I tried \r\n replaced with , and it works but how to simultaneously apply both operations for converting linebreaks to comma and emptyline to be used as linebreaks for obtaining the desired output.

Please help in this regard.
Thanks!

4

2 回答 2

2

将查找和替换置于正则表达式模式。

寻找:

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

用。。。来代替:

$1,

您可以找到它,以消除每行的尾随空格:

([^\r\n]+?) *\r\n
于 2015-11-06T07:54:38.277 回答
0

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

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

用 替换所有这些匹配项,。请注意[ \t]*用于清理每行末尾空白的部分 - 我在您发布的示例中发现了这一点;如果它在现实中不存在,您可以省略该部分。在 regex101.com 上进行实时测试。

然后,全部替换(\r?\n){2,}$1.

于 2015-11-06T07:55:25.790 回答