[英]Regex remove carriage return from end of lines if it exists
The data looks like this: 数据如下所示:
0.43,0.57,0.71,0.29,0.71,1.00,0.57,1.00\r
0.43,0.57,0.71,0.29,0.71,1.00,0.57,1.00
0.43,0.57,0.71,0.29,0.71,1.00,0.57,1.00\r
and I want to extract the carriage return characters (marked with \\r
in the sample above). 我想提取回车符(在上面的示例中用\\r
标记)。 I have been trying this using gm
and a capture group: 我一直在尝试使用gm
和捕获组:
(.*)(?:\\r)$
but this matches only the lines with an \\r
. 但这仅匹配带有\\r
的行。 I thought the solution would be to add a ?
我以为解决方案将添加一个?
before the $
but this does not work. $
之前,但这不起作用。
demo: https://regex101.com/r/jArLdS/1 演示: https : //regex101.com/r/jArLdS/1
Cheers 干杯
If you want to match all lines excluding \\r
- a carriage return - use 如果要匹配\\r
以外的所有行- 回车 -使用
[^\r\n]+
that is a negated character class ( [^...]
) that matches one or more (due to +
quantifier) characters other than \\r
and \\n
. 这是一个否定的字符类 ( [^...]
),它匹配一个或多个\\r
和\\n
以外的(由于+
量)字符。
Portable solution. 便携式解决方案。
(^.*?)(\\\\r)(\\r\\n|\\n)
That's 3 capture groups, left-to-right, starting at 1
, not 0
. 这是三个捕获组,从左到右,从1
开始,而不是0
。
Replace your string with capture groups 1 and 3, skipping 2. 用捕获组1和3替换您的字符串,跳过2。
Capture groups usually syntactically like \\1
or $1
for most languages that use them. 对于大多数使用捕获组的语言,捕获组通常在语法上类似于\\1
或$1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.