Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
C#Linq和Regexing非unicode_C#_Regex_Linq_Unicode - Fatal编程技术网

C#Linq和Regexing非unicode

C#Linq和Regexing非unicode,c#,regex,linq,unicode,C#,Regex,Linq,Unicode,我正在编写一个程序,该程序将从AS400获取数据,并需要读取文本的第一行以确定文件的位置。来自AS400的数据中有许多不可打印的字符 这是我的工作代码: //LINQ to read first line and find what I need var lines = File.ReadAllLines(as400file); foreach (string line in lines) { //Regex the AS400 garbage out of there... s

我正在编写一个程序,该程序将从AS400获取数据,并需要读取文本的第一行以确定文件的位置。来自AS400的数据中有许多不可打印的字符

这是我的工作代码:

//LINQ to read first line and find what I need
var lines = File.ReadAllLines(as400file);
foreach (string line in lines)
{
    //Regex the AS400 garbage out of there...
    string replaced = Regex.Replace(line, @"[^\u0000-\u007F]", String.Empty);
    /*  ^ = not
    *  \u0000 - \u007F is the first 127 chars of UTF-8
    *  So this replaces all non ascii chars with an empty string
    */

    //Rest of program code
}
//LINQ to read first line and find what I need
var lines = File.ReadAllLines(testfile).First(line => !string.IsNullOrWhiteSpace(line));
//Regex the AS400 garbage out of there...
string replaced = Regex.Replace(lines, @"[^\u0000-\u007F]", String.Empty);
/*  ^ = not
 *  \u0000 - \u007F is the first 127 chars of UTF-8
 *  So this replaces all non ascii chars with an empty string
 */
然而,我真的只想要文件的第一行,而不是每一行。我似乎想不出一种方法来获得第一条线,而且我对linq没有那么丰富的经验。有什么建议或帮助吗

var line = File.ReadAllLines(as400file).First(line => !string.IsNullOrWhitespace(line));
string replaced = Regex.Replace(line, @"[^\u0000-\u007F]", String.Empty);

是。。。这就是您想要的吗?

尝试以下操作,它将从文件中读取一行

string line;

using (var file = new StreamReader(as400file))
{
    line = file.ReadLine();
}

string replaced = Regex.Replace(line, @"[^\u0000-\u007F]", String.Empty);

作为Alex回答的替代方案,您可以使用StreamReader仅获取第一行:

using (var reader = new System.IO.StreamReader(as400File))
{
    var line = reader.ReadLine();
    string replaced = Regex.Replace(line, @"[^\u0000-\u007F]", String.Empty);
}

感谢Alex的帮助,以下是我的工作代码:

//LINQ to read first line and find what I need
var lines = File.ReadAllLines(as400file);
foreach (string line in lines)
{
    //Regex the AS400 garbage out of there...
    string replaced = Regex.Replace(line, @"[^\u0000-\u007F]", String.Empty);
    /*  ^ = not
    *  \u0000 - \u007F is the first 127 chars of UTF-8
    *  So this replaces all non ascii chars with an empty string
    */

    //Rest of program code
}
//LINQ to read first line and find what I need
var lines = File.ReadAllLines(testfile).First(line => !string.IsNullOrWhiteSpace(line));
//Regex the AS400 garbage out of there...
string replaced = Regex.Replace(lines, @"[^\u0000-\u007F]", String.Empty);
/*  ^ = not
 *  \u0000 - \u007F is the first 127 chars of UTF-8
 *  So this replaces all non ascii chars with an empty string
 */

我想这就是我想要的,但是有时由于AS400的格式设置,第一行是空白的。我只是简单地添加一个where语句吗?var line=File.ReadAllLines(as400file.First()。其中(line!=null)@moonbeam:First可以使用lambda,因此我将它设置为
。First(line=>!line.IsNullOrWhiteSpace)
我不介意使用第一行,即使它包含lambda,因为我只需要将其正则化。但是,line.IsNullOrWhiteSpace没有选项。在这种情况下,在哪里使用更好?@moonbeam:lambda是一个匿名函数。这是你用来决定第一行是什么的。这就像先做一个where,但效率更高。您可以使用不同的比较而不是.IsNullOrWhitespace,因为这可能是最近添加的,但重要的是在.First中使用lambda会使.Where变得不必要。我想最好不要使用Linq,因为我只是在读取文件,只需要第一行可读的内容。我走哪条路重要吗?