模糊搜索小案例

C#窗体实现数据录入与模糊搜索小案例

记录一下

主要代码

  private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string hometown = textBox4.Text;
            string school = textBox6.Text;
            string sex = textBox5.Text;
            string lat = textBox3.Text;
            string log = textBox2.Text;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] lines = File.ReadAllLines(saveFileDialog1.FileName, Encoding.UTF8); // 如果你需要逐行搜索
                string str=String.Join("\n",lines);
                str += "\n" + name + "," + sex + "," + hometown + "," + school + "," + lat + "," + log + "\n";
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
                sw.Write(str);
                sw.Close();
                
                MessageBox.Show("保存成功!!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string str=textBox7.Text;
            string searchTerm = str; // 正则表达式示例,这里仅匹配独立的“关键字”
            Regex regex = new Regex(searchTerm, RegexOptions.IgnoreCase);
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
     
                string[] lines = File.ReadAllLines(openFileDialog1.FileName, Encoding.UTF8); // 如果你需要逐行搜索
                foreach (var line in lines)
                {
                    Match match = regex.Match(line);
                    if (match.Success)
                    {
                        richTextBox1.Text = line+"\n";
                        MessageBox.Show("找到啦", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

实现思路:
1.数据录入
取textbox数据,打开存储文件,读取数据,添加一行数据,再重新存储数据
2.模糊搜索
正则表达式简单使用,读取数据,使用正则表达式匹配,打印数据

另一种模糊搜索思路:读取数据,建立倒排序索引,建立一个字典,键为单词,值为行索引

在这里插入图片描述

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雲墨知秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值