去除单个换行符,保持“空"线 [英] Remove single line breaks, keep "empty" lines

查看:26
本文介绍了去除单个换行符,保持“空"线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有类似以下用光标选择的文本:

Say I have text like the following text selected with the cursor:

This is a test. 
This 
is a test.

This is a test. 
This is a 
test.

我想把它转换成:

This is a test. This is a test

This is a test. This is a test

换句话说,我想用空格替换单个换行符,单独留下空行.

In other words, I would like to replace single line breaks by spaces, leaving empty lines alone.

我认为以下内容会起作用:

I thought something like the following would work:

RemoveSingleLineBreaks()
{
  ClipSaved := ClipboardAll
  Clipboard =
  send ^c
  Clipboard := RegExReplace(Clipboard, "([^(R)])(R)([^(R)])", "$1$3")    
  send ^v
  Clipboard := ClipSaved
  ClipSaved = 
}

但事实并非如此.如果我将它应用于上面的文本,它会产生:

But it doesn't. If I apply it to the text above, it yields:

This is a test. This is a test.
This is a test. This is a test.

也去掉了中间的空行".这不是我想要的.

which also removed the "empty line" in the middle. This is not what I want.

澄清:空行是指任何带有白色"字符(例如制表符或空格)的行

To clarify: By an empty line I mean any line with "white" characters (e.g. tabs or white spaces)

任何想法如何做到这一点?

Any thoughts how to do this?

推荐答案

RegExReplace(Clipboard, "([^
])R(?=[^
])", "$1$2")

假设新行标记在末尾包含 CRLF(例如 CR、<代码>LF、CR+LFLF+CR).它不会将空格算作空.

This will strip single line breaks assuming the new line token contains either a CR or a LF at the end (e.g. CR, LF, CR+LF, LF+CR). It does not count whitespace as empty.

您的主要问题是 R 的使用:

Your main problem was the use of R:

R 在字符类中只是字母 "R" [来源]

R inside a character class is merely the letter "R" [source]

解决办法是直接使用CRLF字符.

The solution is to use the CR and LF characters directly.

澄清:空行是指任何带有白色"字符(例如制表符或空格)的行

To clarify: By an empty line I mean any line with "white" characters (e.g. tabs or white spaces)

RegExReplace(Clipboard, "(S.*?)R(?=.*?S)", "$1")

这与上面的相同,但将空格视为空.它之所以有效是因为它非贪婪地接受除换行符 (*?) 之外的所有字符,直到换行符前后的第一个非空白字符,因为 . 默认不匹配换行符.

This is the same as the above one, but counts whitespace as empty. It works because it accepts all characters except line breaks non-greedily (*?) up to the first non-whitespace character both behind and in front of the linebreaks, since the . does not match line breaks by default.

前瞻用于避免吃"(匹配)下一个字符,这可能会在单字符行上中断.请注意,由于它不匹配,因此不会被替换,我们可以将其排除在替换字符串之外.不能使用后视,因为 PCRE 不支持可变长度后视,所以在那里使用普通的捕获组和反向引用.

A lookahead is used to avoid 'eating' (matching) the next character, which can break on single-character lines. Note that since it is not matched, it is not replaced and we can leave it out of the replacement string. A lookbehind cannot be used because PCRE does not support variable-length lookbehinds, so a normal capture group and backreference are used there instead.

我想用空格替换单个换行符,单独留下空行.

I would like to replace single line breaks by spaces, leaving empty lines alone.

如果你想用空格替换换行符,这样更合适:

If you want to replace the line break with spaces, this is more appropriate:

RegExReplace(Clipboard, "(S.*?)R(?=.*?S)", "$1 ")

这将用空格替换单个换行符.

This will replace single line breaks with a space.

如果你想使用lookbehinds和lookaheads:

And if you wanted to use lookbehinds and lookaheads:


去除单个换行符:


Strip single line breaks:

RegExReplace(Clipboard, "(?<=[^
	 ][^
])R(?=[^
][^
	 ])", "")


用空格替换单个换行符:


Replace single line breaks with spaces:

RegExReplace(Clipboard, "(?<=[^
	 ][^
])R(?=[^
][^
	 ])", " ")

出于某种原因,S 在后视和前视中似乎不起作用.至少,我的测试不是这样.

For some reason, S doesn't seem to work in lookbehinds and lookaheads. At least, not with my testing.

这篇关于去除单个换行符,保持“空"线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆