【问题标题】:How to remove newline from previous line based on current line's first character?如何根据当前行的第一个字符从上一行中删除换行符?
【发布时间】:2012-08-28 00:19:13
【问题描述】:

我以 CSV 格式导出了一些数据,这些数据在文本字段中有一些换行符。我无法让 Excel 正确处理此问题。我只想编辑文件以删除这些换行符。

有效记录以数字开头。我尝试将\n^([^\d]) 放在 Notepad++ 的查找/替换的“查找内容”框中,以匹配以非数字开头的任何行和前面的换行符。它匹配正确。在“替换为”框中,我放了一个空格,后跟\1 以用空格替换换行符并留下匹配的字符。但是,替换根本不起作用,没有任何改变。

我做错了什么?

示例文本:

123,0,1,"This is a single line comment","bob","jim"
124,0,1,"This is a multi line comment w/ newline.
This is the second line of the comment","ted","alfred"
125,0,1,"This is another single line comment","jim","bob"

我想用空格替换“This is the second...”之前的换行符,使文件看起来像这样:

123,0,1,"This is a single line comment","bob","jim"
124,0,1,"This is a multi line comment w/ newline. This is the second line of the comment","ted","alfred"
125,0,1,"This is another single line comment","jim","bob"

【问题讨论】:

  • 您可以提交您的示例输入文本吗?

标签: regex notepad++


【解决方案1】:

我想通了。我使用(\n)^(?!\d+,\d+) 来匹配任何换行符,其后跟一行开头没有跟至少一个数字、一个逗号和至少一个数字。在“替换为”中,我只是放了一个空格。

【讨论】:

  • 太好了。刚刚在 NotePad++ v6.1.6 中尝试过,它只替换了 LineFeed。由于回车,它仍然停留在下一行。你有哪个版本?
  • Notepad++ 为我做同样的事情,我最终在 Perl 中做了它,它工作得很好,就像这里写的那样。
【解决方案2】:

\n 可以在 Notepad++ 中使用,如果您将换行符设置为 Unix (LF)。 如果它在 Windows (CR LF) 中,那么 \r\n 应该可以工作,或者将返回值转换为 Unix(从底部栏中)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多