简体   繁体   English

正则表达式在Notepad ++中替换管道之间的换行符

[英]Regex to replace newline in between pipes in Notepad++

I am quite new to regex. 我是正则表达式的新手。

I have a pipe-delimited text file that contains free text data. 我有一个以竖线分隔的文本文件,其中包含自由文本数据。 I need to find a way to replace newline characters in the free text fields with commas in Notepad++. 我需要找到一种方法来用Notepad ++中的逗号替换自由文本字段中的换行符。 There is a fixed count of 23 fields. 固定计数为23个字段。

I managed to come up with ^([^\\|]*\\|){22}[^\\|]*$ to identify a full record. 我设法提出了^([^\\|]*\\|){22}[^\\|]*$来标识完整记录。 How do I replace the newlines (\\n) within these strings to collapse all into one liners? 如何替换这些字符串中的换行符(\\ n)以将所有字符折叠成一个衬里?

Sample of data in regex101.com: https://regex101.com/r/aY4jF3/2 Records that start with 1a and 1c need to be collapsed into one line with commas in between field 13's value ( ...|1,3|... ) regex101.com中的数据示例: https ://regex101.com/r/aY4jF3/2以1a和1c开头的记录需要折叠成一行,并在字段13的值之间...(|| 1,3 | ...)

Thanks in advance! 提前致谢!

The regular expression you can use for your replacement is: 可用于替换的正则表达式为:

(^(?:[^\|\n]*\|){1,21}[^\|\n]+)[\r\n]{1,2}

The first group ( (^(?:[^\\|\\n]*\\|){1,21}[^\\|\\n]+) ) captures the lines which have up to 21 pipe characters (so lines that are not full). 第一组( (^(?:[^\\|\\n]*\\|){1,21}[^\\|\\n]+) )捕获最多包含21个竖线字符的行(因此,不完整)。 It's a capturing group as it will be needed for replacement. 这是一个捕获小组,将需要更换。 The rest matches the new line characters. 其余匹配换行符。

And you should replace with 并且您应该替换为

\1,

Which will take the line except the new line characters and add a comma. 除新行字符外,将采用该行并添加逗号。

If you put your cursor at the start of the file and replace all, it will work correctly provided that merged lines will form full lines (with more than 21 pipes). 如果将光标放在文件的开头并替换所有文件,只要合并的行将形成完整的行(具有21个以上的管道),它将可以正常工作。 After the first half is merged with the second, the full line no longer matches the regex and replacement moves on. 在上半部分与第二部分合并之后,整行不再与正则表达式匹配,并且替换继续进行。

Before replacement: 更换前:

在此处输入图片说明

After replacement: 更换后:

在此处输入图片说明

Tested in Notepad++ 6.8.8. 在Notepad ++ 6.8.8中测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM