英文:
Regex to replace multiple spaces at the end of line with comma or add comma
问题
我正在使用正则表达式和TextPad来清理和准备SQL脚本。我想要在以下情况下替换为逗号或添加逗号:
- 每行末尾的一个或多个空格
- 每行末尾或最后一行(即“文件末尾”)
在经过几个小时的研究和迭代后,我得到了以下接近我想要的结果的正则表达式,但不是完美的。如何编辑它以获得以下期望的结果?
查找内容: ' +$|(\n)|$(?![\r\n])'
替换为: '\1\2,'
我的数据看起来像
dog *(2个空格)*
cat *(4个空格)*
bird*(无空格)*
rodent *(1个空格)*
fish*(无空格)*
我希望结果是
dog,
cat,
bird,
rodent,
fish,
我的结果是
dog,
cat,
bird
, rodent,
fish,
英文:
I'm using Regex and TextPad to clean up and prep sql scripts. I want to replace with comma or add comma:
- 1 or more spaces at the end of each line
- at end of each line or the last line (i.e. "end of file")
After spending a few hours researching and iterating I came up with the following which is close to the desired result I want but not perfect. How do I edit this to get the below desired result?
Find what: ' +$|(\n)|$(?![\r\n])'
Replace with: '\1\2,'
I have data that looks like
dog *(2 spaces)*
cat *(4 spaces)*
bird*(no space)*
rodent *(1 space)*
fish*(no space)*
I want the result to be
dog,
cat,
bird,
rodent,
fish,
My result is
dog,
cat,
bird
, rodent,
fish,
答案1
得分: 3
在 textpad 或 notepad++ 中,\s*$
将起作用,但值得一提的是,如果在另一个环境中使用它,可能会导致不期望的匹配 (regex101),并且如果行尾有空格,还会添加额外的逗号。原因是,例如在 cat
中,它将匹配 cat
后面的空格(第一个匹配),以及行尾的零长度匹配(第二个匹配)。
\s*$
的另一个潜在问题可以在这里找到:The RegEx that killed StackOverflow (博客)
如果文本中有许多空格,可能会导致大量的回溯(regex101 演示)。减少步骤的一种解决方法是捕获并捕获直到最后一个非空格字符(如果存在的话)的部分。
^(.*\S)?\s*$
用 $1,
替换(regex101 演示) 第一个组捕获的内容,该组设置为可选的,仅匹配空格。这将将步骤减少到略多于100步。
英文:
In textpad or notepad++ \s*$
will work but it's worth to mention that if using this in another environment it can lead to undesired matches (regex101) and add an extra comma if there are spaces at the end of the line. The reason is that for example in cat
it will match the spaces after cat
(first match) plus a zero-length match at end of the line (second match).
Another potential issue of \s*$
can be read here: The RegEx that killed StackOverflow (blog)
If there are many spaces inside the text it can lead to a lot of backtracking (regex101 demo). This demo input needs about 7k steps just to remove some spaces at the end. A workaround to reduce steps can be to consume and capture the part up to the last non-white-space (if there is one).
^(.*\S)?\s*$
Replace with $1,
(regex101 demo) what's captured by the first group which is set to optional for even matching only whitespace. This would get it down to a bit more than 100 steps.
答案2
得分: 2
Find: \s*$
Replace with: ,
英文:
I think you're overcomplicating it. Just match any number of spaces at the end of the line, and replace with comma.
Find: \s*$
Replace with: ,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论