【问题标题】:Joining line breaks with commas and Removing empty lines to line breaks in Notepade++在 Notepad++ 中用逗号连接换行符并删除空行到换行符
【发布时间】:2015-11-06 07:43:51
【问题描述】:

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

$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 以获得所需的输出。

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

【问题讨论】:

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


    【解决方案1】:

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

    查找:

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

    替换为:

    $1,
    

    你可以找到这个来去掉每一行的尾随空格:

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

    【讨论】:

    • @Bailey--它工作正常,但值之间没有插入逗号,它显示空格
    • @taufel 逗号是由替换部分添加的,所以 $1 是列值,但是在 $1, 之后添加一个逗号。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • @Tim--如何删除白行空格,它在记事本++中的四行之间给出空行
    • 是的,这就是您需要第二步的原因 - 请参阅我回答的最后一行。不可能在单个正则表达式中同时执行这两种操作。
    • @Tim--我已经在 notepad++ 中尝试了第二步,但在第一步之后它不匹配任何东西
    • @Tim--我认为 Bailey 的回答更具体,一步到位
    • @Taufel 对。当我说不可能的事情时,我应该更加小心:)(你应该支持贝利的回答)。
    猜你喜欢
    • 2013-03-22
    • 1970-01-01
    • 2018-01-21
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多