在文件中搜索具有特定格式的字符串的最快方法是什么?

What is the fastest way for search a string with specific format in a file?

我想在文件中找到下面的字符串。实施它的最佳方法是什么?

drop%table_name

star(%) 意味着你可以在 drop 和 table_name

之间有无限的单词

我不想逐字读取文件并处理它。

目前我正在使用 richtextbox 功能来查找一个单词,它工作正常,但我不知道如何处理两个单词和中间的其他单词。请帮助我。

我放了一部分代码,它可以很好地查找一个字符串。

rtb1.Text = utility.readFile(file.Path);

int index = 0;
string searchTxt = "table_name";
while (index < rtb1.Text.LastIndexOf(searchTxt))
{
    rtb1.Find(searchTxt, index, rtb1.TextLength, RichTextBoxFinds.None);
    rtb1.SelectionBackColor = Color.Yellow;
    index = rtb1.Text.IndexOf(searchTxt, index) + 1;
}

输入样本:

drop TABLE mys u_web
/
aLter table  web_cusss rename some other name;
alter table abc

/

drop TABLE mysq weation;
drop TABLE my web_quiciary     /
drop  TABLE web_qudetails;
alter table web_qegy_details modify v_table varchar2(200);
alter table                  web_ition add d_date date;

我的新密码:

 foreach (AMH_FileDetails file in fileList)
            {
                fileIssue += file.ToString();

                if (file.Type.Equals("SQL") || file.Type.Equals("TXT"))
                {
                    string fileBody = utility.readFile(file.Path).ToUpper();
                    foreach (string searchTxt in splittedTblName)
                    {

                        string pattern = @"DROP (.*?) " + searchTxt;
                        string input = fileBody;
                        foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
                        {
                            fileIssue += match.Value + " (drop table " + searchTxt + ") at poition" + match.Index + Environment.NewLine;
                            Console.WriteLine("{0} (drop table '{1}') at position {2}",
                                              match.Value, searchTxt, match.Index);
                        }

                        pattern = @"ALTER\s*TABLE\s*" + searchTxt + "(.*?)[;/]";
                        input = fileBody;
                        foreach (Match match in Regex.Matches(input, pattern, RegexOptions.Multiline))
                        {
                            fileIssue += match.Value + " (Alter table " + searchTxt + ") at poition" + match.Index + Environment.NewLine;
                            Console.WriteLine("{0} (alter table '{1}') at position {2}",
                                              match.Value, searchTxt, match.Index);
                        }

                    }
                    //MessageBox.Show(fileIssue);

                }
                rtb1.Text = fileIssue;
                rtb1.SelectAll();
                rtb1.Copy();
                rtb1.DeselectAll();
            }

您可以使用如下正则表达式轻松完成任务:

string s = "some text drop here some words table_name another text";
var regex = new Regex(@"drop (.*?) table_name");
var match = regex.Match(s);
string words;
if (match.Success)
    words = match.Groups[1].Value;

一些解释。模式 drop (.*?) table_name 匹配以 drop 开头的所有内容,然后是任何符号(由 . 指定)的任何数字(由 * 和 ungreedy 修饰符 ? 指定)直到 table_name.

如果匹配成功,您可以将 droptable_name 之间的单词作为 match.Groups[1].Value,因为我通过指定 (.*?) 将该符号包含在捕获组中在模式中。

您还可以从 match.Groups[0].Value.

中获取整个文本匹配模式

查看 Regex Quick Reference 了解有关 C# 中正则表达式的更多信息。