我正在编写一个正则表达式来查找与文本文件中的Unicode字符匹配的行
!Regex.IsMatch(colCount.line, @"^"[\p{IsBasicLatin}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]"+$")
以下是我编写的完整代码
var _fileName = @"C:\text.txt";
BadLinesLst = File
.ReadLines(_fileName, Encoding.UTF8)
.Select((line, index) =>
{
var count = line.Count(c => Delimiter == c) + 1;
if (NumberOfColumns < 0)
NumberOfColumns = count;
return new
{
line = line,
count = count,
index = index
};
})
.Where(colCount => colCount.count != NumberOfColumns || (Regex.IsMatch(colCount.line, @"[^\p{IsBasicLatin}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]")))
.Select(colCount => colCount.line).ToList();
文件包含以下行
264162-03,66,JITK,2007,12,874.000,0.000,0.000
6420–50-00,67,JITK,2007,12,2292,000,0.000,0.000
4804¥75-00,67,JITK,2007,12,1810.000,0.000,0.000
如果该行的文件包含除BasicLatin或LatinExtended-A或LatinExtended-B之外的任何其他字符,那么我需要获取这些行。上面的正则表达式无法正常工作,这也显示了包含LatinExtended-A或B的那些行
您只需要将Unicode类别类放入否定的字符类中:
if (Regex.IsMatch(colCount.line,
@"[^\p{IsBasicLatin}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]"))
{ /* Do sth here */ }
此正则表达式将查找部分匹配项(因为Regex.IsMatch
finds模式在较大的字符串中匹配)。该模式将匹配\p{IsBasicLatin}
,\p{IsLatinExtended-A}
和\p{IsLatinExtended-B}
Unicode类别集中的任何字符。
您可能还需要检查以下代码:
if (Regex.IsMatch(colCount.line,
@"^[^\p{IsBasicLatin}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]*$"))
{ /* Do sth here */ }
如果整个这将返回truecolCount.line
字符串不包含从否定的字符类指定的3类的Unicode类的任何字符-或-如果字符串是空的(如果你想禁止取空字符串,替换*
与+
末)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句