正则表达式匹配所有不以 ,0 和 ,1 结尾的行 [英] Regex match all lines that don't end with ,0 and ,1
问题描述
我有一个格式错误的 CSV 文件,其中有两列:Text、Value
I have a malformed CSV file which has two columns: Text,Value
该值为 1 或 0,但有些行格式不正确并跨越两行:
The value is either 1 or 0, but some lines are malformed and span two lines:
1. "This line is fine, but there are some that are not like this",0
2. "Another good line",1
4. "Oh, I'm so bad!!
5. I spanned two lines!",0
6. "Why did you break me? FileHelpers can't read two lines!!",1
第 4 行和第 5 行应该是一行,但是我得到的 CSV 文件已损坏并且它们跨越了两行,这导致 FileHelpers 引擎在读取 csv 文件时失败.
Line 4 and 5 are supposed to be one line, but the CSV file I got is broken and they span two lines, this causes the FileHelpers engine to fail while reading the csv file.
我有两个 CSV 文件,每个文件大约有 3000 行,我只需要修复它们一次.我想使用记事本++来查找所有不以 ,0 或 ,1 结尾的行,我可以使用什么样的正则表达式?或者可能是正则表达式,一个用于 ,0 情况,另一个用于 ,1 情况.
I have two CSV files with about 3000 lines each and I will only need to fix them once. I want to use notepad++ to find all the lines that are not ending in ,0 or ,1, what kind of regex can I use for that? Or maybe to regular expressions, one for the ,0 case the other one for the ,1 case.
更新:
Dan 的答案没有逗号 [^01]$ 而不是 ,[^01]$,但它只匹配不以 0 或 1 结尾的行......在我的情况下它工作得很好,但它确实跳过了那些行被破坏,实际上以 0 或 1 结尾.
Update:
Dan's answer works without the comma [^01]$ instead of ,[^01]$, but it only matches lines that are not ending with 0 or 1... it works sufficiently well in my case, but it does skip lines that are broken and actually end with 0 or 1.
推荐答案
我不知道其他答案会如何工作:
I don't know how the other answer would work:
我会在 Notepad++ 中使用类似下面的内容
Something like the below is what I would use in Notepad++
[^,][^01]$
这是我做的步骤:
使用 ([^,][^01])$
匹配行并替换为 \1{marked}
Use ([^,][^01])$
to match the lines and replaced with \1{marked}
然后切换到扩展模式并将 {marked}\r\n
替换为 ``(空)以获得单行.
Then switched to extended mode and replaced {marked}\r\n
with `` ( empty ) to get a single line.
截图如下:
这篇关于正则表达式匹配所有不以 ,0 和 ,1 结尾的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!