Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在正则表达式中使用Unicode_Asp.net_Regex_Unicode - Fatal编程技术网

Asp.net 如何在正则表达式中使用Unicode

Asp.net 如何在正则表达式中使用Unicode,asp.net,regex,unicode,Asp.net,Regex,Unicode,我正在编写一个正则表达式来查找与文本文件中的Unicode字符匹配的行 !Regex.IsMatch(colCount.line, @"^"[\p{IsBasicLatin}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]"+$") 下面是我写的完整代码 var _fileName = @"C:\text.txt"; BadLinesLst = File .ReadLines(_fileName, Encoding.UTF8)

我正在编写一个正则表达式来查找与文本文件中的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();
var\u fileName=@“C:\text.txt”;
BadLinesLst=文件
.ReadLines(_文件名,Encoding.UTF8)
.选择((行、索引)=>
{
var count=line.count(c=>Delimiter==c)+1;
if(NumberOfColumns<0)
NumberOfColumns=计数;
还新
{
行=行,
计数=计数,
索引=索引
};
})
.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,12874.000,0.000,0.000

6420œ50-00,67,JITK,2007,122292.000,0.000,0.000

4804¥75-00,67,JITK,2007,121810.000,0.000,0.000

若行的文件包含除BasicLatin或LatinExtended-A或LatinExtended-B之外的任何其他字符,那个么我需要获取这些行。
上面的正则表达式工作不正常,这也显示了那些包含LatinExtended-A或B的行

您只需将Unicode类别类放入一个:

这个正则表达式将查找部分匹配(因为
regex.IsMatch
在较大的字符串中查找模式匹配)。该模式将匹配任何字符,而不是
\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 */ }

如果整个
colCount.line
字符串不包含否定字符类中指定的3个Unicode类别中的任何字符-或者-如果字符串为空(如果不允许提取空字符串,请在末尾将
*
替换为
+
)。

修改正则表达式后,在一行中,我保留了一个字符。但是正则表达式也与该行匹配,但在实际场景中,正则表达式不应与包含扩展应用程序的行匹配。请共享与正则表达式匹配但不应匹配的字符串。此外,我建议使用一些Unicode转换器(如)检查字符属于哪个Unicode类别。480Œ475-00,67,JITK,2007,121810.000,0.000,0.000在480之后,我保留了LatinExtended-A字符,这一行不应该与正则表达式匹配,因为我提到,它应该忽略LatinExtended-A,使用此列表选择上面显示的字符串。两者也不匹配。也可以在上查看IDEONE演示。您在中共享的任何链接我都显示正确,但对于我来说,我不知道它为什么不起作用,我已经更新了问题并添加了我编写的完整代码什么是
NumberOfColumns
Delimiter
?分隔符是,(逗号),如果我不传递列数,则需要-1。假设我的行有,分隔的列,所以我检查所有行是否有相同的列数,并使用regex查找包含spcl字符或chines字符的行,除了提及regexI检查,同时删除这行代码,但它也不工作..好吧,我尝试了一个包含
480Œ475-00,67,JITK,2007,121810.000,0.000,0.000
фы
。这不是预期的吗?请注意,编码始终是棘手的部分。如果不是ANSI,您只需要将
true
传递给
StreamReader
,如果不是,您应该始终注意默认代码页将与
编码一起使用。默认值
if (Regex.IsMatch(colCount.line, 
     @"^[^\p{IsBasicLatin}\p{IsLatinExtended-A}\p{IsLatinExtended-B}]*$")) 
{ /* Do sth here */ }