[英]Regex - remove any number of commas at the end of a line
Alright, Regex gurus, how can I change my logic to fix this one? 好吧,Regex专家,我该如何更改自己的逻辑以解决此问题?
I've made a regex: 我做了一个正则表达式:
(,[,]+)
It's supposed to remove extra commas on the end of a line. 应该删除行尾的多余逗号。 (end of line being \\r\\n) when formatted as a string.
(行尾为\\ r \\ n),格式为字符串时。
It works (sort of). 它有效(有点)。
This is the string: 这是字符串:
Date,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24,\r\nDate,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24,,,,,\r\nDate,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24,,,,,\r\nDate,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24,,\r\n
When I run that regex, it gives a result of: 当我运行该正则表达式时,其结果为:
Date,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24,\r\nDate,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24\r\nDate,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24\r\nDate,1-Jul-18,1-Jul-19,1-Jul-20,1-Jul-21,1-Jul-22,1-Jul-23,1-Jul-24\r\n
I need to remove the comma at the end of the first line (I think I need to be finding \\r\\n and killing any commas before that, until a non-comma. 我需要删除第一行末尾的逗号(我想我需要先找到\\ r \\ n并杀死所有逗号,直到出现非逗号为止。
Any thoughts about how to do this? 有关如何执行此操作的任何想法?
Thanks 谢谢
(,+$)
perhaps? (,+$)
也许? (One or more commas followed immediately by the end of a line.) (一个或多个逗号,紧接在行尾。)
如果您的语言支持正向前进,请尝试以下操作-
([,]*)(?=\\r\\n)
I think you can match one or more ,
followed by \\r\\n
by using ,+\\\\r\\\\n
. 我认为您可以匹配一个或多个
,
然后使用,+\\\\r\\\\n
匹配\\r\\n
,+\\\\r\\\\n
。 Don't know how to replace that using C# sorry. 不知道如何使用C#替换它。 In perl I would do
在Perl中我会做
perl -pi -e 's/,+\\r\\n/\\r\\n/g' c.txt
(assuming that c.txt
is a file containing your input text). (假设
c.txt
是包含您输入文本的文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.