我有这样的文本文件..。
$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转换为comma,并将emptyline用作linebreaks以获得所需的输出。
请在这方面提供帮助。
谢谢!
发布于 2015-11-06 07:54:38
将查找和替换放入regex模式。
查找:
([^\r\n]+)\r\n代之以:
$1,相反,您可以找到这一点,以消除每一行上的尾部空间:
([^\r\n]+?) *\r\n发布于 2015-11-06 07:55:25
你需要分两步做。首先,用逗号替换所有的换行符,但前提是它们不在行的开头,而且只有在以下情况下才会出现$字符:
(?<!^)[ \t]*\r?\n(?=\$)用,替换所有这些匹配。注意,用于清除每行末尾空白的[ \t]*部件--我发现在您发布的示例中;如果它在实际中不存在,则可以省略该部分。测试它在regex101.com上直播。
然后,将所有(\r?\n){2,}替换为$1。
https://stackoverflow.com/questions/33561786
复制相似问题