从文件中获取一个单词,然后获取下一个单词.C#.
本文关键字:单词 获取 然后 一个 下一个 文件 | 更新日期: 2023-09-27 18:34:46
我在这里需要一些建议。我正在制作一个应用程序来获取VB表单中的特定SQL语句,但不知道哪个是这样做的最佳选择。
例如,我有这句话来解释:
FROM sys.master_files WITH (NOLOCK)
但我需要的是
FROM sys.master_files
我需要得到FROM
和接下来的事情,比如sys.master_files
第一刻这正是我所需要的,它是一个基本的应用程序,只是为了抓住 FROM 和接下来的事情。
现在我一直在研究和查看 StackOverFlow 中已经提出的一些问题,我想知道Regex.Match
是否是进行此检查的更快方法,或者我是否可以使用该IndexOf
但我不太了解它是如何工作的。
这就是我到目前为止对代码所做的。
private void btnSearch_Click(object sender, EventArgs e)
{
string value = cboParam.Text;
string[] reservedWords = { "FROM", "SELECT", "JOIN", "UPDATE", "DROP", "ALTER", "CREATE" };
if (cboParam.Text == "")
{
MessageBox.Show("Wrong try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
List<string> listParam = new List<string>();
listParam.Add(value);
if (selectedPath == null && openFileDialog.FileNames == null)
{
MessageBox.Show("Choose a directory please.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (selectedPath != null)
{
foreach (string str in selectedPath)
{
string split = " ";
string readText = File.ReadAllText(str)
.Replace("'n", split)
.Replace("'r", split)
.Replace("_", split)
.Replace("-", split)
.Replace("SQL", split)
.Replace("sql", split)
.Replace("FROM", split)
.Replace("from", split)
.Replace("JOIN", split)
.Replace("join", split)
.Replace("UPDATE", split)
.Replace("update", split)
.Replace("DELETE", split)
.Replace("delete", split);
readText.IndexOf(value);
}
}
else if (openFileDialog.FileNames != null)
{
foreach (string str in openFileDialog.FileNames)
{
string split = "'nGO";
string readText = File.ReadAllText(str)
.Replace("'n", split)
.Replace("'r", split)
.Replace("_", split)
.Replace("-", split)
.Replace("SQL", split)
.Replace("sql", split)
.Replace("FROM", split)
.Replace("from", split)
.Replace("JOIN", split)
.Replace("join", split)
.Replace("UPDATE", split)
.Replace("update", split)
.Replace("DELETE", split)
.Replace("delete", split);
readText.IndexOf(value);
}
}
}
}
这就是我所拥有的,我试图使用 IndexOf 但无法提供连续性,因为我不了解它的操作。
为了更好地理解我来自巴西,所以我将代码的对变量更改为英语,以"易于"理解代码。
这是正则表达式的任务:
var input = "FROM sys.master_files WITH (NOLOCK) FROM sys1.master_files1 WITH (NOLOCK)";
var rg = new Regex(@"FROM [a-zA-Z0-9'._]+ ");
var result = rg.Matches(input);
如果您正在寻找正则表达式来获取该部分:
string s = System.Text.RegularExpressions.Regex.Match("test FROM sys.master_files WITH (NOLOCK)", "FROM ([^ ]*)").Groups[1].Value;
使用 Linq 的另一个解决方案:
string xx = "test FROM sys.master_files WITH (NOLOCK)";
string yy = xx.Split(' ').SkipWhile(x => x != "FROM").Take(2).Last();
而且我敢肯定还有更多。
我使用
您的标题来理解这个问题,它说您需要字符串的前两个单词。使用 IndexOf 和子字符串:
string original = "FROM sys.master_files WITH (NOLOCK)";
string firstTwoWords = original.Substring(0, original.IndexOf(' ', original.IndexOf(' ')+1));
应该让你从sys.master_files
第二行的作用是获取原始字符串的一部分,该字符串从索引 0 开始,并以空格的第二次出现结束。但这不适用于单词之间的多个空格(意外编写(。
因此,您可以执行以下操作来处理空白区域:
string original = "FROM sys.master_files WITH (NOLOCK)";
string[] array = original.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string newString = array[0] +' '+ array[1];