C# 从文件中获取两个哈希之间的文本 [英] C# get text from file between two hashes

查看:45
本文介绍了C# 从文件中获取两个哈希之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 C# 程序中(此时)我的表单中有两个字段.一种是使用列表框的单词列表;另一个是文本框.我已经能够成功地将一个大的单词列表从文本文件加载到列表框中.我还可以通过这种方式将列表框中的选定项目显示到文本框中:

private void wordList_SelectedIndexChanged(object sender, EventArgs e){字符串字 = wordList.Text;concordanceDisplay.Text = word;}

我有另一个本地文件需要访问才能在文本框中显示其某些内容.在这个文件中,每个词条(如在字典中)都以# 开头.因此,我想使用变量word"并在此本地文件中搜索以将条目放入文本框中,如下所示:

<块引用>

#headword1入口在这里.........#headword2入口在这里.........#headword3入口在这里.........

你得到文本文件的格式.我只需要在该词之前用 # 搜索正确的词条,然后从那里复制所有信息,直到文件中的下一个哈希值,然后将其放入文本框中.

显然,我是新手,所以要温柔.非常感谢.

附言我使用 StreamReader 获取单词列表并将其显示在列表框中,如下所示:

StreamReader sr = new StreamReader("C:\\...\\list-final.txt");字符串线;while ((line = sr.ReadLine()) != null){MyList.Add(line);}wordList.DataSource = MyList;

解决方案

string getSection(string sectionName){StreamReader sr = new StreamReader(@"C:\Path\To\file.txt");字符串线;var MyList = new List();bool inCorrectSection = false;while ((line = sr.ReadLine()) != null){if (line.StartsWith("#")){如果(错误部分)休息;别的inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"($| -)");}否则如果(inCorrectSection)MyList.Add(line);}返回 string.Join(Environment.NewLine, MyList);}//在另一种方法中textBox.Text = getSection("headword1");

以下是检查部分是否匹配的几种替代方法,按它们检测正确部分名称的准确程度的粗略顺序:

//如果节名后面的分隔符总是-",这是我想到的最好的方法,因为不管节名中是什么它都会工作inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"($| -)");//只要部分名称不能包含# 或空格,这将起作用inCorrectSection = line.Split('#', '')[1] == sectionName;//只要只有字母数字字符可以组成部分名称,这很好inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"\b");//问题在于,如果您正在搜索head",它会找到headOther"并认为它是匹配的inCorrectSection = line.StartsWith("#" + sectionName);

In my C# program (at this point) I have two fields in my form. One is a word list using a listbox; the other is a textbox. I have been able to successfully load a large word list into the listbox from a text file. I can also display the selected item in the listbox into the textbox this way:

private void wordList_SelectedIndexChanged(object sender, EventArgs e)
     {
          string word = wordList.Text;
          concordanceDisplay.Text = word;
     }

I have another local file I need to get at to display some of its contents in the textbox. In this file each headword (as in a dictionary) is preceded by a #. So, I would like to take the variable 'word' and search in this local file to put the entries into the textbox, like so:

#headword1
    entry is here...
    ...
    ...
#headword2
    entry is here...
    ...
    ...
#headword3
    entry is here...
    ...
    ...

You get the format of the text file. I just need to search for the correct headword with # before that word, and copy all info from there until the next hash in the file, and place it in the text box.

Obviously, I am a newbie, so be gentle. Thanks much.

P.S. I used StreamReader to get at the word list and display it in the listbox like so:

StreamReader sr = new StreamReader("C:\\...\\list-final.txt");
       string line;
       while ((line = sr.ReadLine()) != null)
       {
           MyList.Add(line);
       }
       wordList.DataSource = MyList;

解决方案

string getSection(string sectionName)
{
    StreamReader sr = new StreamReader(@"C:\Path\To\file.txt");
    string line;
    var MyList = new List<string>();
    bool inCorrectSection = false;
    while ((line = sr.ReadLine()) != null)
    {
        if (line.StartsWith("#"))
        {
            if (inCorrectSection)
                break;
            else
                inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"($| -)");
        }
        else if (inCorrectSection)
            MyList.Add(line);
    }
    return string.Join(Environment.NewLine, MyList);
}

// in another method
textBox.Text = getSection("headword1");

Here are a few alternate ways to check if the section matches, in rough order of how accurate they are in detecting the right section name:

// if the separator after the section name is always " -", this is the best way I've thought of, since it will work regardless of what's in the sectionName
inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"($| -)");
// as long as the section name can't contain # or spaces, this will work
inCorrectSection = line.Split('#', ' ')[1] == sectionName;
// as long as only alphanumeric characters can ever make up the section name, this is good
inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"\b");
// the problem with this is that if you are searching for "head", it will find "headOther" and think it's a match
inCorrectSection = line.StartsWith("#" + sectionName);

这篇关于C# 从文件中获取两个哈希之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆